Syntax for creating a two-dimensional array

前端 未结 12 1203
悲&欢浪女
悲&欢浪女 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 18:45

    In Java, a two-dimensional array can be declared as the same as a one-dimensional array. In a one-dimensional array you can write like

      int array[] = new int[5];
    

    where int is a data type, array[] is an array declaration, and new array is an array with its objects with five indexes.

    Like that, you can write a two-dimensional array as the following.

      int array[][];
      array = new int[3][4];
    

    Here array is an int data type. I have firstly declared on a one-dimensional array of that types, then a 3 row and 4 column array is created.

    In your code

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

    means that you have created a two-dimensional array, with five rows. In the first row there are 10 columns. In Java you can select the column size for every row as you desire.

提交回复
热议问题