Array initialisation in java

前端 未结 5 1937
忘掉有多难
忘掉有多难 2020-12-01 23:20

I noticed one could write code like this, which is perfectly normal, by the way:

int arrays[] = {1, 2, 3};
for (int n : arrays)
   System.out.println(n);
         


        
5条回答
  •  执念已碎
    2020-12-02 00:07

    Because arrays are objects, and have to be instantiated. Java does not recognize {} by itself as an array. It does, however, permit you to use a code block (i.e. code in {..}) to define the initial elements of the array.

    The way you accomplish this is described by the answer above.

提交回复
热议问题