2D Arrays
Day 1: 8.1.1
- Arrays can store many items of the same type
- You can store items in
two-dimensional
arrays- arrays with rows and columns
- You can store items in
- 2d arrays are especially useful when data is naturally organized in rows and columns
- spreadsheet, theater seats, games, etc.
Array Storage: 8.1.2
- Many languages store 2d arrays in a 1d array
- Store all data for all rows sequentially
- row-major order
- Store all data for columns sequentially
- column-major order
- Store all data for all rows sequentially
2D array
0 | 1 | 2 | |
0 | 2 | 4 | 8 |
1 | 8 | 10 | 12 |
row major order
0 | 1 | 2 | 3 | 4 | 5 |
2 | 4 | 6 | 8 | 10 | 12 |
column major order
0 | 1 | 2 | 3 | 4 | 5 |
2 | 8 | 4 | 10 | 6 | 12 |
How Java Stores 2D Arrays: 8.1.3
- Java stores a 2d array as an array of arrays
- Each element of an outer array has a reference to each inner array
- The outer array are the rows
- The inner array is the columns!
- Inner arrays can have different lengths
ragged arrays
- Assume that any 2d array on the exam is row-major
Declaring 2D Arrays: 8.1.4
- Specify the type, then
[][]
to indicate the array is 2 dimensional, then a name- You still need to initialize seperately!
- To create an array, use
new
new int[rows][columns]
- The number of elements in a 2d array is the number of rows times the number of columns
int[][] sudoku = new int[2][3];
Set value(s) in a 2D Array: 8.1.5
- When arrays are created, their values are initialized as normal
- To explicitly set a value, use an assignment statement with a row and column index
int[][] sudoku = new int[2][3];
seatingChart[0][2] = 39;
Initializer Lists: 8.1.6
- You can initialize the values of an array when you create it
- You don’t need to specify the size
- It is automatically set from the number of values
- You don’t need to specify the size
int[][] sudoku = {{0, 1, 2},
{3, 4, 5},
{6, 7, 9}};
Get a Value from a 2D Array: 8.1.7
- to get a value from a 2d array, give the name, followed by the row and column indices in brackets
int myVal = arr[row][column];
Summary: 8.1.9
- A 2D array is stored as an array of arrays
- 2D arrays are created and indexed similarly to 1D arrays
- 2D arrays are declared and created as follows
dataType[][] varName = new dataType[rows][columns];
- Ragged arrays are outside the scope of the AP exam
- For the purposes of the exam, when accessing the element at
arr[first][second]
,first
is used for rows,second
is used for columns - The initializer list for 2D arrays consists of initializer lists for 1D arrays
int[][] ticketInfo = { {25,20,25}, {25,20,25} };
- Row-major order refers to an ordering where traversal occurs across each row
- column-major order is when traversal is down each column
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.