-2

I am looking for a simple way to set the elements of an array after it is initialized. I have tried this :

package com.ehsan.app;

public class Main {

    public static void main(String[] args) {
        int[] test = new int[6];
        test = {1,2,3,4,5};
    }
}

However compiling this gives error :

Error:(7, 16) java: illegal start of expression
Error:(7, 17) java: not a statement
Error:(7, 18) java: ';' expected
Error:(9, 1) java: class, interface, or enum expected

I can use this way to put values in the array:

test[0] = 1;
test[1] = 2;
test[2] = 3;
test[3] = 4;
// and so on.

I am just looking for a simple way to do that.


Edit

I know I can use this :

int[] test = {1,2,3,4};

But what I want is a simple way to put values in array after its initialization.

And one another thing : I am not looking for loops!


Edit

The answer @Python gave was what I was looking for!

4
  • 2
    please rewrite your question, after initialized array, only you can give value by position with help of loop, or a[i], when i=1,2,......N. Commented Jun 27, 2016 at 5:48
  • if you are doing this , test = {1,2,3,4,5}; means you don't know what are valid statements as far as array initialization is concerned - this is not IDE issue but an invalid Java statement. Why putting values in array elements via loop doesn't look easier to you? Commented Jun 27, 2016 at 5:58
  • if you array length is dynamic, than without loop, you can go for recursive. Commented Jun 27, 2016 at 6:04
  • I am not familiar with C# but in Java, once you have allocated memory to an array variable , there is no one liner shorthand to put all values for that array. You will have to use API classes as listed in answer by Python Commented Jun 27, 2016 at 6:24

5 Answers 5

0

You can simply do this by using Arrays's static method in one line:

Integer[] test = new Integer[10];
Arrays.<Integer>asList(1,2,3,4,5,6,7).toArray(test);

Note: using 'int' is also fine but then you to add explicit casting (int[])

Sign up to request clarification or add additional context in comments.

2 Comments

This looks cool, thanks
Just a reminder for this approach: For most other approaches here, if test is shorter than the "initialization list", you will still populate into test with just enough elements (and ignore the rest from the "initialization list"). However, for this approach, it will be silently ignored and test will stay uninitialized.
0

If you want to initialize an array, try using Array Initializer:

int[] test = new int[6];
int[] test = {1,2,3,4,5,6};

OR

int[] test = new int[]{1,2,3,4,5,6};

2 Comments

If this is not you are looking for, you need to rewrite your question because it is unclear what your are looking for.
it seems he has edited his question, you may delete your comment.
0

Initialize using a for loop:

for(int i=0;i<test.length-1;i++){
 test[i]=i;
}

this will make the test array have, 0,1,2,3,4,5 as its contents;

Comments

0

I believe there is no other way than having a loop to populate your already-instantiated array.

However, you don't need to write the loop by yourself:

System.arrayCopy() does the work. And one day JVM may decide to do something like memcpy to do the work, by using such kind of built-in functions, you may benefit from such JVM change without change in your source code.

Comments

0

After initializing you can't assign values all at once, you can do it this way :

test[0]=1;
test[1]=2;

and likewise.. hope this will help

4 Comments

So there isn't an easier way to do that !?
just use a for loop for more convenience..
Sure, I know that, was just looking for some kinda initializer list after initialization!, Thanks.
for(int i=0;i<test.length;i++){ test[i]=i+1; } this is all u've been looking for.. cheers.