Week 7 part 2
A two dimensional array of ints has been created and assigned to a2d. Write an expression whose value is the number of rows in this array
a2d.length
A two dimensional array of ints has been created and assigned to a2d. Write an expression whose value is the number of elements in the first row. (Assume array is not empty)
a2d[0].length
A two dimensional array of ints with 4 rows has been created and assigned to a2d. Write an expression whose value is the total number of ints that could be stored in the entire array.
a2d[0].length + a2d[1].length + a2d[2].length + a2d[3].length
A two dimensional array of ints has been created and assigned to a2d. Write an expression whose value is the number of elements in the last row. (Assume array is not empty)
a2d[a2d.length - 1].length
Declare and create a 2D array of chars, tictactoe, with 3 rows, each with 3 elements, and initialize it to all space characters.
char[][] tictactoe = {{' ', ' ', ' '}, { ' ', ' ', ' '}, {' ', ' ', ' '}};
Write a statement to declare a variable named x for a two dimensional array of double values
double[] [] x;
Syntax for two dimensional array
elementType[] [] arrayRefVar Ex(matrix variable of int values): int[] [] matrix;
A 2D 3x3 array of ints has been created and assigned to tictactoe. Write an expression whose value is that of the first element in the first row.
tictactoe[0][0]
A 2D 3x3 array of ints has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the first row are all equal
tictactoe[0][0] == tictactoe[0][1] && tictactoe[0][0] == tictactoe[0][2]
A 2D 3x3 array of ints has been created and assigned to tictactoe. Write an expression whose value is that of the middle element in the middle row.
tictactoe[1][1]
A 2D 3x3 array of ints has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the last row are all equal
tictactoe[2][0] == tictactoe[2][1] && tictactoe[2][0] == tictactoe[2][2]
Write an expression to obtain the row size of a 2D array x and an expression to obtain the size of the first row
x.length x[0].length Practice: int[][] m = {{4, 5, 6}, {2, 1, 0}}, what is m.length and m[0].length? 2 3
How many elements are in array matrix (int[][] matrix = new int[5][5])?
25 Hint: it is a square matrix 5 by 5
Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?
3, 2
Assume double[][] x = new double[4][5], what are x.length and x[2].length?
4 and 5
Suppose int[] [] m = {{4,5,6}, {2,1,0}}, what is m[0][1]?
5 hint: refer to Goodnotes 2 and 3
Array initializer
A shorthand notation to, declare, create and initialize an array: (2/3 in Goodnotes)
Two subscripts
One for the row (row index) and one for the column(column index). The index for each subscript is of the int typed and starts from 0.
Declare an array reference variable for a two dimensional array of int values, create a 4 x 5 int matrix, and assign it to the variable
int[] [] = new int[4] [5];
Declare and create a two dimensional array of ints, plan, with 2 rows, and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75.
int[] [] plan = { {8, 20, 50}, {12, 30, 75} };
Assign value 7 to a specific element at row index 2 and column index 1
matrix[2] [1] = 7; Note: it's a common mistake to use matrix[2, 1] to access the element at row 2 and column 1.
Write the expression for creating a two-dimensional array of the int type with 40 rows and 20 columns
new int[40] [20];
Two dimensional array
An array in which each element is a one dimensional array
Two dimensional arrays
An element in a two dimensional array is accessed through a row and a column index
Ragged array
Each row in a 2D array is itself an array. Thus, rows can have different lengths.
numbers.length - 1
If u have an array called "numbers" with 10 elements, then numbers.length would be 10 and numbers.length -1 would be 9, which is the index of the last element in the array. The first element has an index of 0, so .length - 1 gives you the index of the last element in the array
Declare a two-dimensional array of strings named chessboard
String[][] chessboard;
Obtaining length of 2D arrays
The length of an array x is the number of elements in the array. Use .length Ex: x = new int[3][4], x[0], x[1], and x[2] are 1D arrays and each contains 4 elements x.length is 3, x[0].length, x[1].length, x[2].length are 4. Hint(Goodnotes)
When you create an array using: int[] [] matrix = new int[5][5]; The element values are automatically initialized to 0.
True