4.11 2D Array Creation and Access

1 阅读2分钟

1. Exam Points

  • create and initialize 2D arrays: use the new keyword or an initializer list.
  • access elements in a 2D array: arrayName[rowIndex][columnIndex]
  • Get the number of rows: arrayName.length
  • Get the number of columns in one row: arrayName[rowIndex].length
  • default values for uninitialized elements:
    • int: 0
    • double: 0.0
    • boolean: false
    • referece type: null
  • Index out of range: ArrayIndexOutOfBoundsException.

2. Knowledge Points

(1) 1D Array vs. 2D Arrays

  • 1D Array
    • One row, multiple columns:
    • Use one index(starting at 0) to represent the position of an element.
    • Example:
    
    int[] nums = {1, 2, 3};
    
    System.out.println(nums[0]);
    System.out.println(nums[1]);
    
  • 2D Array: An array of arrays (数组的数组).
    • Multiple rows, multiple columns. Like a matrix.
      image.png
    • Example:
    int[][] nums = 
    {
        {1,  2,  3,  4},
        {5,  6,  7,  8},
        {9, 19, 11, 12},
    };
    System.out.println(nums[0][0]);
    System.out.println(nums[1][0]);
    System.out.println(nums[2][3]);
    
    • Use two indexes, the first is the row index, and the second the column index. And both the row index and the column index starts from 0.
      • nums[0][0] means 1st row, 1st column.
      • nums[2][3] means 3rd row, 4th column.

(2) Create 2D Arrays

  • Similar to 1D arrays, 2D arrays can store either primitive data or object references.
  • Similar to 1D arrays, the size of a 2D array is established at the time of creation and cannot be changed.
  • Two ways of creating a 2D array:
    • Use the new keyword:
      image.png
    • Use an initializer list:
      image.png
  • Similar to 1D arrays, when a 2D array is created using the keyword new, all of its elements are initialized to the default values for the element data type.
    • int: 0
    • double: 0.0
    • boolean: false
    • reference type(String, Dog): null

(3) Access Elements of 2D Arrays

  • Similar to 1D arrays, we use "arrayName + index" to access elements in a 2D array.
    • Syntax: arrayName[rowIndex][columnIndex]
    • Example:
    
    int[][] nums = new int[3][4];
    nums[0][0] = 1;
    nums[0][1] = 2;
    
    System.out.println(nums[0][1]); // print 2
    System.out.println(nums[2][3]); // print the default value for int: 0
    
    System.out.println(nums[3][3]); // ArrayIndexOutOfBoundsException
    
  • If either the row index or column index is out of range, an ArrayIndexOutOfBoundsException rises.

(4) Get number of rows and columns

  • In 1D arrays, the length attribute can be used to get the size (number of elements) of an 1D array.
    • Example:
    int[] nums = {1, 2, 3};
    System.out.println(nums.length); // print 3 (number of elements)
    
  • In 2D arrays, arrayName.length returns the number of rows of a 2D array.
  • In 2D arrays, the number of columns can be accessed using the length attribute of each row.
  • Example:
    int[][] nums = { 
        {1,  2,  3}, 
        {5,  6,  7, 8}
    };
    
    System.out.println(nums.length); // print 2, number of rows in the 2D array
    System.out.println(nums[0].length); // print 3, number of columns in the 1st row
    System.out.println(nums[1].length); // print 4, number of columns in the 2nd row
    

(5) 2D Arrays as parameters or return types

  • Similar to 1D arrays, 2D arrays can be used as parameters or return types.
  • Example:
    
    // a method to print a specified element of a given 2D array.
    public static void print(int[][] arr, int rowIndex, int colIndex) {
        System.out.println(arr[rowIndex][colIndex]);
    }
    
    public static void main(String[] args) {
    
        int[][] nums = { 
            { 1, 2, 3, 4 }, 
            { 5, 6, 7, 8 }, 
            { 9, 10, 11, 12 } 
        };
    
        // 1. print the first element in a 2D array
        print(nums, 0, 0);
    
        // 2. print the element in the second row, first column
        print(nums, 1, 0);
    
        // 2. print the last element in a 2D array
        print(nums,2, 3);
        print(nums,nums.length-1, nums[0].length-1);
    

3. Exercises