2D and 3D Arrays in Java

Understanding Array of Arrays - The Computer Science Way

CPCS203 - Object-Oriented Programming | Week 1

1 The Problem: Storing Student Marks

Imagine you are building a system to store marks for 3 students in 4 courses. How would you store this data?

Approach 1: Using Separate Variables

You could create a variable for each student's mark in each course:

S1C1
Student1, Course1
S1C2
Student1, Course2
S1C3
Student1, Course3
S1C4
Student1, Course4
S2C1
Student2, Course1
S2C2
Student2, Course2
S2C3
Student2, Course3
S2C4
Student2, Course4
S3C1
Student3, Course1
S3C2
Student3, Course2
S3C3
Student3, Course3
S3C4
Student3, Course4
Problem!

This requires 3 × 4 = 12 variables! What if you have 100 students and 10 courses? That's 1000 variables! This is completely impractical.

Approach 2: Using Separate Arrays

A better idea: Create one array for each student to hold their course marks:

int[] student1 =
85
90
78
92
int[] student2 =
76
88
95
82
int[] student3 =
90
85
88
91
Still a Problem!

This is better, but what if you have 100 students? You would need 100 separate array variables! This is the same problem as before - too many variables to manage.

The Solution: Array of Arrays

What if we could have ONE array that contains all the student arrays?

This is exactly what a 2D array is - an array of arrays!

2 The Computer Only Understands 1D Arrays!

Critical Understanding for Data Structures!

The computer does NOT understand 2D or 3D arrays directly. The computer only understands single-dimensional (1D) arrays.

A "2D array" in Java is actually an array of references, where each reference points to another 1D array!

Let's visualize how a 2D array is actually structured in memory:

2D Array: marks[3][4] - An Array of Arrays
STACK marks 0x50 HEAP 0x50 (Array of References) [0] 0x100 [1] 0x200 [2] 0x300 0x100 (Student 0's marks) [0] 85 [1] 90 [2] 78 [3] 92 0x200 (Student 1's marks) [0] 76 [1] 88 [2] 95 [3] 82 0x300 (Student 2's marks) [0] 90 [1] 85 [2] 88 [3] 91 Legend Reference var Array of refs 1D Data Arrays: Student 0 Student 1 Student 2 Each 1D array is independent! marks stores reference 0x50

Understanding the Structure:

  • The first dimension (vertical) is an array that stores references (addresses)
  • Each reference points to a separate 1D array (the actual data)
  • The second dimension arrays can be stored anywhere in memory - they don't need to be next to each other!

3 Creating a 2D Array: Step by Step

Let's see how a 2D array is created in three steps:

1
Declaration - Create the Reference Variable
Step1_Declaration.java
1int[][] marks;  // Only declaration

This creates a reference variable called marks. It currently holds null - no array exists yet!

marks
null
2
Create the First Dimension (Array of References)
Step2_FirstDimension.java
1int[][] marks = new int[3][];  // Create first dimension only

This creates an array of 3 references. Each reference is currently null - no data arrays exist yet!

marks
0x50
[0] null
[1] null
[2] null
3
Create the Second Dimension (Data Arrays)
Step3_SecondDimension.java
1// Create array for each student
2marks[0] = new int[4];  // Array for student 0
3marks[1] = new int[4];  // Array for student 1
4marks[2] = new int[4];  // Array for student 2

Now we create the actual data arrays! Each marks[i] now points to a real array of 4 integers.

Step 3 Diagram

Shorthand: All in One Line

ShorthandCreation.java
1// All three steps in one line:
2int[][] marks = new int[3][4];  // 3 students, 4 courses
3
4// Or with initialization:
5int[][] marks = {
6    {85, 90, 78, 92},  // Student 0's marks
7    {76, 88, 95, 82},  // Student 1's marks
8    {90, 85, 88, 91}   // Student 2's marks
9};

4 Accessing Elements in a 2D Array

To access an element, you need two indices:

marks
[
i
]
[
j
]
i = which array (student)
j = which element in that array (course)
AccessElements.java
1int[][] marks = {
2    {85, 90, 78, 92},  // marks[0] - Student 0
3    {76, 88, 95, 82},  // marks[1] - Student 1
4    {90, 85, 88, 91}   // marks[2] - Student 2
5};
6
7// Access Student 1's mark in Course 2
8int mark = marks[1][2];  // Result: 95
9
10// Get the number of students (first dimension)
11int numStudents = marks.length;  // Result: 3
12
13// Get the number of courses for student 0
14int numCourses = marks[0].length;  // Result: 4
Understanding .length
  • marks.length = number of arrays (first dimension) = 3
  • marks[i].length = size of array i (second dimension) = 4

5 3D Arrays: Array of Arrays of Arrays

Following the same logic, a 3D array is an array of 2D arrays, which means it's an array of arrays of arrays!

Real-World Example

Imagine you need to store marks for multiple semesters:

  • 2 semesters
  • 3 students per semester
  • 4 courses per student

This is 2 × 3 × 4 = 24 values organized in a 3D structure!

ThreeDArray.java
 1// 3D Array: 2 semesters × 3 students × 4 courses
 2int[][][] allMarks = new int[2][3][4];
 3
 4// Or with initialization:
 5int[][][] allMarks = {
 6    // Semester 0
 7    {
 8        {85, 90, 78, 92},  // Student 0
 9        {76, 88, 95, 82},  // Student 1
10        {90, 85, 88, 91}   // Student 2
11    },
12    // Semester 1
13    {
14        {88, 92, 80, 95},  // Student 0
15        {79, 90, 97, 85},  // Student 1
16        {93, 87, 90, 94}   // Student 2
17    }
18};

Visualizing 3D Array Structure

3D Array: allMarks[2][3][4] - Array of 2D Arrays (Array of Arrays of Arrays!)
STACK allMarks 0x10 HEAP 0x10 (Semesters) [0] 0x100 [1] 0x200 Semester 0 (0x100) - Array of Student References Students [0] 0x1A [1] 0x1B [2] 0x1C 0x1A (Courses) [0] 85 [1] 90 [2] 78 [3] 92 0x1B [0] 76 [1] 88 [2] 95 [3] 82 0x1C [0] 90 [1] 85 [2] 88 [3] 91 Semester 1 (0x200) - Array of Student References Students [0] 0x2A [1] 0x2B [2] 0x2C 0x2A (Courses) [0] 88 [1] 92 [2] 80 [3] 95 0x2B [0] 79 [1] 90 [2] 97 [3] 85 0x2C [0] 93 [1] 87 [2] 90 [3] 94 Step 1: allMarks Reference allMarks stores reference 0x10 → points to the Semesters Array Step 2: Semesters Array (at 0x10) This array has 2 references: [0] = 0x100 → Semester 0's Students [1] = 0x200 → Semester 1's Students Step 3: Students Arrays Each semester has an array of 3 students. Each student reference (0x1A, 0x1B, ...) points to that student's courses array. Step 4: Courses Arrays (Actual Data) Each student has 4 course marks. This is where the actual int values live!

Accessing 3D Array Elements

allMarks
[
sem
]
[
stu
]
[
course
]
sem = which semester
stu = which student
course = which course
Access3D.java
1// Get Student 1's mark in Course 2 during Semester 0
2int mark = allMarks[0][1][2];  // Result: 95
3
4// Dimensions:
5int semesters = allMarks.length;           // 2
6int students = allMarks[0].length;        // 3
7int courses = allMarks[0][0].length;     // 4

6 Summary

Key Takeaways for Data Structures
  1. The computer only understands 1D arrays - there is no "true" 2D or 3D array in memory
  2. 2D array = Array of Arrays - the first dimension holds references to 1D arrays
  3. 3D array = Array of 2D Arrays - the first dimension holds references to 2D arrays
  4. Each dimension is created separately - understanding this is crucial for data structures
  5. The arrays can be anywhere in memory - they are connected by references, not by being physically adjacent

Why This Matters for Data Structures:

When you study linked lists, trees, and graphs next semester, you'll see that they all work on the same principle - elements connected by references, not by being next to each other in memory. Understanding "array of arrays" is your first step toward mastering these concepts!