Syntax for creating a two-dimensional array

前端 未结 12 1489
悲&欢浪女
悲&欢浪女 2020-11-21 18:11

Consider:

int[][] multD = new int[5][];
multD[0] = new int[10];

Is this how you create a two-dimensional array with 5 rows and 10 columns?<

12条回答
  •  天命终不由人
    2020-11-21 19:05

    The most common idiom to create a two-dimensional array with 5 rows and 10 columns is:

    int[][] multD = new int[5][10];
    

    Alternatively, you could use the following, which is more similar to what you have, though you need to explicitly initialize each row:

    int[][] multD = new int[5][];
    for (int i = 0; i < 5; i++) {
      multD[i] = new int[10];
    }
    

提交回复
热议问题