0

I am 100% sure this already has an answer somewhere, but I've not been able to find it...

I've created an array and specified its length. Later I want to fill it with values, but I can't use the

array = {1, 2, 3, 4}

syntax any more. There's no pattern to the values going in the array, so they can't be put into a for loop where

for(i=0;i<array.length;i++){
     array[i] = someValue[i]
}

Can I avoid writing out array[i] = someValue manually for each i? Basically, is there anything similar to

array = {1,2,3,4}

That I could use, where I just fill the array in one line? If this is incredibly unclear, which it might be, let me know and I'll post the actual code I'm using.

user13948
  • 443
  • 1
  • 6
  • 14
  • 2
    You can assign to an existing array variable a new array with a list of contents, as `array = new int[] { ... };`. Is that close enough? – khelwood Dec 13 '15 at 10:55
  • @khelwood That's exactly what I was after. Thanks! – user13948 Dec 13 '15 at 10:56
  • 1
    Also note that since you'll be replacing the value of your `int[]` field/variable anyway, it's redundant to "create an array and specify its length" first; the original array would just get garbage-collected. So simply declare your field/variable as `int[] array;` with no initial value. – Jonik Dec 13 '15 at 11:06
  • @Jonik Thanks, didn't know that was possible, thought it would result in a compiler error! – user13948 Dec 13 '15 at 11:31

3 Answers3

4

You can use this

array = new int[]{1,2,3,4};
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
2

Just to clarify, when you do array = new int[]{1, 2}; as in this comment and this answer, it creates a new array, discarding the array that array initially pointed to.

Nothing wrong with that, but note that then it is redundant to first "create an array and specify its length", as you said; the original array just gets garbage-collected. So it makes more sense to simply declare your field/variable as int[] array; with no initial value.

On the other hand, if you for some reason specifically want to reuse your original array object, filling it with values you come up with later, I think in general you'd have to go with a loop and assigning to array[i] "manually". One exception though: to fill you array (or a part of it), with the same value, you can use the built-in Arrays.fill() utility. For example:

int[] array = new int[5];
Arrays.fill(array, 1);
// array now contains [1, 1, 1, 1, 1]           

// Only fill a specified range:
int[] array2 = new int[10];
Arrays.fill(array2, 3, 7, 2);
// array2 now contains [0, 0, 0, 2, 2, 2, 2, 0, 0, 0]
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • Well, I had to declare it at class level to avoid scoping issues. So yes, I used the int[] array declaration without setting an initial value, and it worked very well. Thanks! – user13948 Dec 13 '15 at 13:19
  • (All that said about arrays, I’d like to add that for most purposes you really should instead use the higher-level, more user-friendly APIs from the [Collections framework](http://docs.oracle.com/javase/tutorial/collections/index.html). Supplemented, if you like, by the [Stream API](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) for functional-style operations on Java 8.) – Jonik Dec 13 '15 at 14:39
1

You will need to re-initialize the array using:

array = new int[] { 1, 2, 3, 4 } 

You cannot use the Array Initializer syntax in an assignment, it is only allowed when initializing an array.

Orestis P.
  • 805
  • 7
  • 27