Which of the following returns the number of columns in a two dimensional array?

This book is now obsolete Please use CSAwesome instead.

Arrays know their length (how many elements they can store). It is a public read-only field so you can use dot-notation to access the field (arrayName.length). The length of the outer array is the number of rows and the length of one of the inner arrays is the number of columns.

Note

Note that length is a field and not a method, so you don’t add parentheses after length. However, if you use parentheses after length during the exam, you won’t lose any points.

ticketInfo.length // returns the number of rows ticketInfo[0].length // returns the number of columns

Note

Since for the AP CS A exam all two-dimensional arrays are rectangular arrays (arrays that have the same number of columns in each row) you can just use the length of the first inner array as the number of columns as shown by ticketInfo[0].length.

Check your understanding

    9-3-1: How many rows does a have if it is created as follows int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4} };?

  • 2
  • The size of outer list is the number of rows.
  • 4
  • The size of the inner list is the number of columns.
  • 8
  • This is the total number of items in the array.

    9-3-2: Which of the following would I use to get the value in the third row and second column from a 2D array called nums?

  • nums[3][2]
  • This would be true if array indices started with 1 but they start with 0.
  • nums[2][3]
  • This would be true if array indicies started with 1 and the column was specified first. However, array indices start at 0 and the row is given first in row-major order.
  • nums[2][1]
  • Array indices start with 0 so the third row has an index of 2 and the second column has an index of 1.
  • nums[1][2]
  • This would be true if the column index was first, but in row-major order the row index is first.

Since you can find out the number of rows and columns in a 2D array you can use a nested for loop (one loop inside of another loop) to loop through all of the elements of a 2D array.

public class Test { public static double getAverage(int[][] a) { double total = 0; int value = 0; for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[0].length; col++) { value = a[row][col]; total = total + value; } } return total / (a.length * a[0].length); } public static void main(String[] args) { int[][] matrix = { {1,2,3},{4,5,6} }; System.out.println(getAverage(matrix)); } }

Some key things to notice about this code are:

  • total is declared to be a double so that the result will be a double. If total was declared to be an int then the result would be an integer and the values after the decimal point would be thrown away.

  • The number of rows is a.length

  • The number of columns is a[0].length

  • The number of times this loop executes is the number of rows times the number of columns.

You can step through the code by clicking on this link1

Mixed up programs

The following has the correct code to find the largest value in a 2D array. Drag the blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong order or have the wrong indention.

public static int getLargest(int[][] arr) { --- int largest = arr[0][0]; int current = 0; for (int r = 0; r < arr.length; r++) { --- for (int c = 0; c < arr[0].length; c++) { --- current = arr[r][c]; if (current > largest) { --- largest = current; --- } // end if --- } // end column loop --- } // end row loop return largest; --- } // end method

You can step through this code using the Java Visualizer by clicking on the following link2

Since 2D arrays are really arrays of arrays you can also use a nested for-each loop to loop through all elements in an array. Loop through each of the inner arrays and loop through all the values in each inner array.

public class Test { public static double getAvg(int[][] a) { double total = 0; for (int[] innerArray : a) { for (int val : innerArray) { total = total + val; } } return total / (a.length * a[0].length); } public static void main(String[] args) { int[][] theArray = { {80, 90, 70}, {20, 80, 75} }; System.out.println(getAvg(theArray)); } }

In this case the for (int[] colArray : a) means to loop through each element of the outer array which will set colArray to the current column array. Then you can loop through the value in the column array.

You can step through this code using the Java Visualizer by clicking on the following link3

16

New! Save questions or answers and organize your favorite content.
Learn more.

For an array in Java, we can get the length of the array with array_name.length. Similarly, how would one get the number of rows and columns of a 2D array?

0