Syntax for creating a two-dimensional array

前端 未结 12 1218
悲&欢浪女
悲&欢浪女 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:48
    int [][] twoDim = new int [5][5];
    
    int a = (twoDim.length);//5
    int b = (twoDim[0].length);//5
    
    for(int i = 0; i < a; i++){ // 1 2 3 4 5
        for(int j = 0; j <b; j++) { // 1 2 3 4 5
            int x = (i+1)*(j+1);
            twoDim[i][j] = x;
            if (x<10) {
                System.out.print(" " + x + " ");
            } else {
                System.out.print(x + " ");
            }
        }//end of for J
        System.out.println();
    }//end of for i
    
    0 讨论(0)
  • 2020-11-21 18:48

    These types of arrays are known as jagged arrays in Java:

    int[][] multD = new int[3][];
    multD[0] = new int[3];
    multD[1] = new int[2];
    multD[2] = new int[5];
    

    In this scenario each row of the array holds the different number of columns. In the above example, the first row will hold three columns, the second row will hold two columns, and the third row holds five columns. You can initialize this array at compile time like below:

     int[][] multD = {{2, 4, 1}, {6, 8}, {7, 3, 6, 5, 1}};
    

    You can easily iterate all elements in your array:

    for (int i = 0; i<multD.length; i++) {
        for (int j = 0; j<multD[i].length; j++) {
            System.out.print(multD[i][j] + "\t");
        }
        System.out.println();
    }
    
    0 讨论(0)
  • 2020-11-21 18:50

    Try the following:

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

    ... which is a short hand for something like this:

    int[][] multi = new int[5][];
    multi[0] = new int[10];
    multi[1] = new int[10];
    multi[2] = new int[10];
    multi[3] = new int[10];
    multi[4] = new int[10];
    

    Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:

    int[][] multi = new int[][]{
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    };
    
    0 讨论(0)
  • 2020-11-21 18:52

    You can create them just the way others have mentioned. One more point to add: You can even create a skewed two-dimensional array with each row, not necessarily having the same number of collumns, like this:

    int array[][] = new int[3][];
    array[0] = new int[3];
    array[1] = new int[2];
    array[2] = new int[5];
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
提交回复
热议问题