开发者

Java: How initialize an array in Java in one line?

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


array1 = {1, 1, 1, 1, 2, 5, ,7, 8}; - NOT worki开发者_开发技巧ng

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?


array = new int[] {1, 1, 2, 3, 5, 8};

Source: Oracle JavaDocs - Arrays


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜