开发者

Is the Braces in the following statement means an open array?

开发者 https://www.devze.com 2023-01-26 05:52 出处:网络
Integer[] ints = list.toArray(new Integer[]{}); 开发者_如何学运维If I remove \"{}\" the compiler asks to fill in a dimension for the array. What do the two braces mean as a command?It means you init
Integer[] ints = list.toArray(new Integer[]{});
开发者_如何学运维

If I remove "{}" the compiler asks to fill in a dimension for the array. What do the two braces mean as a command?


It means you initialize the array with what is in between the braces. Ex:

new Integer[] { 1, 2, 3}

Makes an array with 1, 2 and 3. On the other hand:

new Integer[] {}

Just mean that you initialize an array without any values. So it is the same as new Integer[0].


This actually means empty array. The {} allow you to supply the elements of the array:

Integer[] ints = list.toArray(new Integer[]{1, 2, 3});

is equivalent to:

Integer[] ints = new Integer[3];
ints[0] = 1;
ints[1] = 2;
ints[2] = 3;

Check this link:

  • http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

for more information - go to Creating, Initializing, and Accessing an Array section.


Yes, an empty array. Just like Integer[0].

0

精彩评论

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