tags:

views:

104

answers:

2
int[] array1 = {1, 2, 3, 4, 5, 6, ,7, 8}; - working


array1 = {1, 1, 1, 1, 2, 5, ,7, 8}; - NOT working

The first line is working, but second line is not working.

How can I make the initialization from the second line in one single line of code?

+11  A: 
array = new int[] {1, 1, 2, 3, 5, 8};
MikeD
Add to your answer link to the documentation: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
uthark
It is the "new" that is important.
Thorbjørn Ravn Andersen
@uthark: I don't see where this syntax is on the page that you linked. The closest it comes is the array copying at the bottom, but that's not exactly a one line solution. Can you be more specific?
MikeD
+3  A: 

The reason the first one works is because the compiler can check how many elements you are going to assign to the array, and then allocate the appropriate amount of memory.

EDIT: I realize now that you are just trying to update array1 with new data... Mike D's answer solves that.

Dolph