1. Exam Points
create and initialize 2D arrays: use thenewkeyword or aninitializer list.access elements in a 2D array:arrayName[rowIndex][columnIndex]Get the number of rows: arrayName.lengthGet the number of columns in one row: arrayName[rowIndex].lengthdefault 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 ArrayOne 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.
- 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, thefirst is the row index, andthe second the column index. And both therow indexand thecolumn 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 arrayscan store eitherprimitive dataorobject references. - Similar to 1D arrays, the
sizeof a 2D array is established at the time of creation andcannot be changed. Two ways of creating a 2D array:- Use the
newkeyword:
- Use an
initializer list:
- Use the
- Similar to 1D arrays, when a 2D array is created using the keyword
new, all of its elements are initialized to thedefault valuesfor 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 - Syntax:
- If either the
row index or column index is out of range, anArrayIndexOutOfBoundsExceptionrises.
(4) Get number of rows and columns
- In
1D arrays, thelength attributecan be used to get thesize(number of elements) of an 1D array.- Example:
int[] nums = {1, 2, 3}; System.out.println(nums.length); // print 3 (number of elements) - Example:
- In
2D arrays,arrayName.lengthreturns thenumber of rowsof 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 arrayscan be used asparameters 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);