Syntax for creating a two-dimensional array

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

    Try this way:

    int a[][] = {{1,2}, {3,4}};
    
    int b[] = {1, 2, 3, 4};
    
    0 讨论(0)
  • 2020-11-21 18:40

    Actually Java doesn't have multi-dimensional array in mathematical sense. What Java has is just array of arrays, an array where each element is also an array. That is why the absolute requirement to initialize it is the size of the first dimension. If the rest are specified then it will create an array populated with default value.

    int[][]   ar  = new int[2][];
    int[][][] ar  = new int[2][][];
    int[][]   ar  = new int[2][2]; // 2x2 array with zeros
    

    It also gives us a quirk. The size of the sub-array cannot be changed by adding more elements, but we can do so by assigning a new array of arbitrary size.

    int[][]   ar  = new int[2][2];
    ar[1][3] = 10; // index out of bound
    ar[1]    = new int[] {1,2,3,4,5,6}; // works
    
    0 讨论(0)
  • 2020-11-21 18:44

    We can declare a two dimensional array and directly store elements at the time of its declaration as:

    int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};
    

    Here int represents integer type elements stored into the array and the array name is 'marks'. int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type.

    Coming back to our statement written above: each row of elements should be written inside the curly braces. The rows and the elements in each row should be separated by a commas.

    Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. These blocks can be individually referred ta as:

    marks[0][0]  marks[0][1]  marks[0][2]  marks[0][3]  marks[0][4]
    marks[1][0]  marks[1][1]  marks[1][2]  marks[1][3]  marks[1][4]
    marks[2][0]  marks[2][1]  marks[2][2]  marks[2][3]  marks[2][4]
    


    NOTE:
    If you want to store n elements then the array index starts from zero and ends at n-1. Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator.

    int marks[][];           // declare marks array
    marks = new int[3][5];   // allocate memory for storing 15 elements
    

    By combining the above two we can write:

    int marks[][] = new int[3][5];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 18:46

    Try:

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

    Note that in your code only the first line of the 2D array is initialized to 0. Line 2 to 5 don't even exist. If you try to print them you'll get null for everyone of them.

    0 讨论(0)
  • 2020-11-21 18:48

    It is also possible to declare it the following way. It's not good design, but it works.

    int[] twoDimIntArray[] = new int[5][10];
    
    0 讨论(0)
提交回复
热议问题