Syntax for creating a two-dimensional array

前端 未结 12 1197
悲&欢浪女
悲&欢浪女 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:54

    int rows = 5;
    int cols = 10;
    
    int[] multD = new int[rows * cols];
    
    for (int r = 0; r < rows; r++)
    {
      for (int c = 0; c < cols; c++)
      {
         int index = r * cols + c;
         multD[index] = index * 2;
      }
    }
    

    Enjoy!

提交回复
热议问题