2D Arrays Summary: 8.3.0

  • You learned about two dimensional arrays
    • Stores objects in a 2D table
    • Creates an array of arrays
    • Learned how to declare, create, and access 2D arrays and their elements
  • Array elements are accessed using row and column indices
    • First element is at [0][0]

Concept Summary: 8.3.1

  • 2D array
    • An array which holds items in a 2d grid
    • you can access an item using a row and column index
  • 2D Array declaration
    • You can declare an array by specifying the type, then [][], then a name
    • int[][] seats;
    • String[][] seatingChart;
  • 2D array creation
    • Create a 2D array by typing the name and an equals sign
    • use new keyword, then the type, and the number of rows and columns
    • seatingChart = new String[5][4];
  • 2D Array Index
    • You can access and set values in a 2D array using a row and column index
    • arr[0][0]
  • 2D Array Initialization
    • You can initialize a 2D Array when you create it
    • The size is automagically determined
    • int[][] myInt = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
  • 2D array number of rows
    • The length of the outer array
    • arr.length
  • 2D array number of columns
    • The length of an inner array
    • arr[0].length
  • nested for loop
    • A for loop inside another for loop
  • out of bounds error
    • when a loop goes beyond the last valid index in an array
      • the last valid row is arr.length - 1!

Common Mistakes: 8.3.3

  • Forgetting to create an array; only declaring it
  • Using 1 as the first index
    • is 0
  • using array[0].length as the last valid column
    • not array[0].length - 1
  • using array.length() instead of array.length
  • going out of bounds when looping through an array
    • Throws an ArrayIndexOutOfBoundsException!
  • Jumping out of a loop using a return statement before the end of the loop
  • using the incorrect starting or ending indices
  • using array.length for both rows and columns
    • use array[0].length for columns