-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!

  • 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. – ankush yadav Jun 27 '16 at 05: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? – Sabir Khan Jun 27 '16 at 05:58
  • if you array length is dynamic, than without loop, you can go for recursive. – ankush yadav Jun 27 '16 at 06: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 – Sabir Khan Jun 27 '16 at 06:24

5 Answers5

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};
Geeky Ninja
  • 5,765
  • 8
  • 35
  • 50
  • If this is not you are looking for, you need to rewrite your question because it is unclear what your are looking for. – Geeky Ninja Jun 27 '16 at 05:47
  • it seems he has edited his question, you may delete your comment. –  Jun 27 '16 at 10:17
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

Dinidu Hewage
  • 1,977
  • 6
  • 39
  • 45
Utkarsh
  • 50
  • 1
  • 7
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;

Bot
  • 1
  • 1
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.

Adrian Shum
  • 35,318
  • 9
  • 72
  • 119
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[])

PyThon
  • 957
  • 7
  • 22
  • This looks cool, thanks –  Jun 27 '16 at 06:19
  • 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. – Adrian Shum Jun 28 '16 at 03:03