How is this illegal

前端 未结 5 1492
南方客
南方客 2020-12-12 06:12
a) int i[] = new int[2]{1,2};
b) int[] i = new int[]{1,2,3}{4,5,6}; 

I know we cannot give size of an array at declaration . But in statement(a) we

5条回答
  •  臣服心动
    2020-12-12 06:39

    Both are illegal!

    a) You either specify the size or the content. Legal would be:

    int i[] = new int[2];
    i[0] = 1;
    i[1] = 2;
    

    or

    int i[] = new int[]{1,2};
    

    b) 2-dimensional arrays are arrays containing arrays. So, you have to write:

    int[] i = new int[][]{{1,2,3}, {4,5,6}};
                         ^       ^        ^
    

提交回复
热议问题