For int[][] m = new int[3][4];, m.length and m[0].length are:
Multidimensional Arrays
Declaring and creating 2D arrays, initializers, lengths, ragged arrays, the processing loops (input, random, print, sum, column sums), passing 2D arrays to methods, and 3D arrays.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 20 slides
- 1 Multidimensional Arrays
- 2 Motivations
- 3 Motivations
- 4 Declare/Create Two-dimensional Arrays
- 5 Declaring Variables of Two- dimensional Arrays and Creating Two-dimensional Arrays
- 6 Two-dimensional Array Illustration
- 7 Declaring, Creating, and Initializing Using Shorthand Notations
- 8 Lengths of Two-dimensional Arrays
- 9 Lengths of Two-dimensional Arrays, cont.
- 10 Ragged Arrays
- 11 Ragged Arrays, cont.
- 12 Processing Two-Dimensional Arrays
- 13 Initializing arrays with input values
- 14 Initializing arrays with random values
- 15 Printing arrays
- 16 Summing all elements
- 17 Summing elements by column
- 18 Passing Tow-Dimensional Arrays to Methods
- 19 Multidimensional Arrays
- 20 Multidimensional Arrays
Declare/Create Two-dimensional Arrays
Notes
int[][] matrix = new int[5][5]; matrix[0][0] = 3;
Lengths of Two-dimensional Arrays
Notes
matrix.length = number of rows; matrix[0].length = columns of row 0.
Exam angle
Summing all elements
Notes
Sum all: nested for over rows and columns.
Summing elements by column
Notes
Column sums: outer loop over columns, inner over rows.
Exam angle
Write code that sums each column of int[][] m.
Compare with the model answer
for (int c = 0; c < m[0].length; c++) { int total = 0; for (int r = 0; r < m.length; r++) total += m[r][c]; System.out.println(total); }
Passing Tow-Dimensional Arrays to Methods
Notes
Pass a 2D array: public static int sum(int[][] m).
Multidimensional Arrays
Notes
3D arrays: double[][][] scores = new double[6][5][2];