# 2D Arrays

• 2D Arrays stands for 2-Dimensional Arrays.

• A way to explain 2D Array’s is to use an example of a vending machine.

• So you go to a vending machine and you see the different types of snacks, you know which type you want, and when you put in your money you hope for the snack to drop out.

• The snack machine can be thought of as a set of rows across in the machine with different types of snacks like chips, and granola bars.

• The vertical columns will contain each separate type of snack, so column 1 will just be all chips, and column 2 will just be all granola bars.

• The result of this is an “array of arrays”, or in other words a 2D array.

• The indexes for each item are assigned individually for each row and column’s location.

• For example, say you want to get the granola bar in row 3, and it’s in the second column you code would look like this: `vendingMachine[2][1]`

• Note: Remember that Indexes start at 0 on the AP Exam!

• Note: Remember that Index numbers end at array.length-1.

• The methods that are used in arrays are quite similar to those in 2D arrays, because a 2D array, is just an array, except more sophisticated.

• Important lines of code to remember to figure out the number of rows and columns:

• Rows length: `vendingMachine.length`

• This would tell you the number of slots that are across the machine.

• Columns length: `vendingMachine[0].length`

• This would tell you the length of the first column.

• Ex:

``//Consider the following code segment, and what it will print out as a resultint [][] numbers = {{1,2,3,4},{5,6,7,8}};for(int[] nums: numbers)  for(int n: nums)     System.out.print(n + " ");System.out.print("\n");``
• Answer: It will print 1, 2, 3, 4, 5, 6, 7, 8. This is an enhanced for loop. The first loop uses the idea that a 2D array is an array of arrays. It goes one row at a time and stores it in nums. The inner loop takes one element at a time from the row and stores it in n. There are no line breaks, so the array is printed one element after the other.

• Ex:

``/*The following class, Arr2d, is meant to represent a 2 dimensional array object.the constructor will initialize, Arr2d using the number of rows and columns that have been passedChoose the statement that will initialize the array in the constructor*/public class Arr2D{   private int [][] arr;      Arr2D( int rows, int columns)   {     /*missing code*/   }}//what should the missing code be?/*a. int [] arr = new String [rows][columns];b. int [][] arr = new String [rows-1][columns-1];c.arr = new String [rows][columns];d.arr = new String [rows-1][columns-1];e.int arr [][] = new String [rows][columns];*/``
• Answer: The variable identified as arr has been created, which eliminates A, B, and E. The indices of arrays start at 0, and the range is always one less than the number of rows or columns, and the declaration needs to use the exact number of rows and columns that are desired in the specific array. Which makes the right answer to be C.