π4.11: 2D Array Creation & Access
Table of Contents
π This page is a condensed version of CSAwesome Topic 4.11
β΄β΄β΄ NEW UNIT/SECTION! β΄β΄β΄
Create a blank Java program to take your class notes in for the next few lessons.
Click on the collapsed heading below for GitHub instructions ‡
π NOTES PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository
- Specify the repository name:
CS2-Unit4PartC-Notes
- For the description, write:
2D Array data collections
- Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositories
section of GitHub! π - Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
and wait for the environment to load, then youβre ready to code! - π Take notes in this Codespace during class, writing code & comments along with the instructor.
π When class ends, donβt forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.
There are multiple steps to saving in GitHub Codespaces:
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java
- Click the small
βοΈ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
2D Arrays
A 2D array is an array of arrays, useful for representing grids, tables, or matrices. In Java, a 2D array is declared as:
int[][] matrix;
You can think of a 2D array as rows and columns, like a spreadsheet.
Declaring and Initializing 2D Arrays
You can declare and initialize a 2D array in several ways:
int[][] nums = new int[3][4]; // 3 rows, 4 columns
This creates a 3-by-4 grid of integers, all initialized to 0.
You can also initialize with values:
int[][] nums = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Accessing Elements
Use two indices: the first for the row, the second for the column.
System.out.println(nums[1][2]); // prints 7
Rows and columns are zero-indexed.
Traversing a 2D Array
Nested for loops are commonly used to traverse 2D arrays.
for (int row = 0; row < nums.length; row++) {
for (int col = 0; col < nums[row].length; col++) {
System.out.print(nums[row][col] + " ");
}
System.out.println();
}
Enhanced for Loops with 2D Arrays
You can use enhanced for
loops for each row, then for each element in the row.
for (int[] row : nums) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
Practice: Fill a 2D Array
Instructions: Create a 3x3
integer 2D array and fill it with numbers from 1 to 9 in order. Then print it using nested loops.
public class FillArray {
public static void main(String[] args) {
int[][] nums = new int[3][3];
// Fill with numbers 1 to 9
// Print the array
}
}
2D Arrays with Objects
2D arrays can hold objects as well as primitives.
String[][] seatingChart = new String[2][3];
seatingChart[0][0] = "Alice";
seatingChart[0][1] = "Bob";
Jagged Arrays
In Java, 2D arrays are arrays of arrays, so each row can have a different length.
int[][] jagged = {
{1, 2},
{3, 4, 5},
{6}
};
Example: Tic-Tac-Toe Board
Create a 3x3 String
2D array to represent a tic-tac-toe board. Fill it with "-"
to represent empty spaces, then print it.
public class TicTacToe {
public static void main(String[] args) {
String[][] board = new String[3][3];
// Fill and print board
}
}
Summary
- A 2D array is an array of arrays:
type[][] name
. - Access elements with
array[row][col]
. - Traverse with nested loops.
- Rows can have different lengths (jagged arrays).
- Can store primitives or objects.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.