In the last lesson, every array was a single row of items. But real data isn't always a single row. A class timetable has rows and columns. A list of students keeps growing as new admissions come in.
Different situations call for different kinds of arrays.
By the end of this lesson, you will understand:
- One-dimensional and two-dimensional arrays
- Multi-dimensional arrays
- The difference between static and dynamic arrays
- How dynamic arrays grow behind the scenes
- What a jagged array is
1. One-Dimensional Arrays
This is the array you already know: a single row of items, reached with one index.
index: 0 1 2 3 4
+----+----+----+----+----+
| 72 | 85 | 90 | 64 | 88 |
+----+----+----+----+----+
Use it for any simple list: marks of one student, prices of items, temperatures for a week.
2. Two-Dimensional Arrays
A two-dimensional (2D) array is a grid, made of rows and columns, like a table in a spreadsheet or the seats in a cinema hall. You reach an item with two indexes: [row][column].
Here are the marks of 3 students in 3 subjects:
column 0 column 1 column 2
(Maths) (Physics) (English)
+----------+----------+----------+
row 0 → | 72 | 85 | 90 | student 0
+----------+----------+----------+
row 1 → | 64 | 88 | 79 | student 1
+----------+----------+----------+
row 2 → | 91 | 76 | 83 | student 2
+----------+----------+----------+
marks[1][2] means row 1, column 2: student 1's English mark, which is 79.
To visit every item in a grid, you use a loop inside a loop: the outer loop goes through the rows, and the inner loop goes through each column of that row.
public class Main {
public static void main(String[] args) {
// 3 students (rows) × 3 subjects (columns)
int[][] marks = {
{72, 85, 90}, // student 0
{64, 88, 79}, // student 1
{91, 76, 83} // student 2
};
System.out.println(marks[1][2]); // 79 (row 1, column 2)
for (int row = 0; row < marks.length; row++) {
for (int col = 0; col < marks[row].length; col++) {
System.out.print(marks[row][col] + " ");
}
System.out.println();
}
}
}All four print:
79
72 85 90
64 88 79
91 76 83
Because there is a loop inside a loop, visiting every item of an r × c grid takes O(r × c) steps.
3. Multi-Dimensional Arrays
You can keep adding dimensions. A three-dimensional array is like a stack of grids, for example marks[class][student][subject] for several classes. You would read one item with three indexes, like marks[2][1][0].
In practice, 1D and 2D arrays cover the vast majority of problems. Beyond three dimensions, the data usually becomes hard to picture and is better organised another way.
4. Static vs Dynamic Arrays
This is one of the most important differences between arrays.
Static Arrays
A static array has a fixed size that is decided when you create it. If you create space for 5 items, you can never store a 6th. A Java int[] and a C array are static.
Dynamic Arrays
A dynamic array grows automatically as you add items. You don't need to know the size in advance. Java's ArrayList, Python's list and JavaScript's Array are all dynamic.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>(); // starts empty
marks.add(72);
marks.add(85);
marks.add(90); // grows as needed
System.out.println(marks); // [72, 85, 90]
System.out.println(marks.size()); // 3
}
}How Does a Dynamic Array Grow?
Remember from the last lesson: an array's items must sit side by side in memory. So when a dynamic array runs out of room, it can't just stretch. Instead it:
- Reserves a new, bigger block of memory, usually double the size
- Copies every existing item across
- Adds the new item, and frees the old block
The C example above does exactly this by hand. In Java, Python and JavaScript, it happens automatically.
Copying everything sounds slow, but because the space doubles each time, it happens rarely. Most additions just drop into a free slot, which is why adding to the end of a dynamic array is O(1)on average.
| Static array | Dynamic array | |
|---|---|---|
| Size | Fixed when created | Grows as you add items |
| Java | int[] | ArrayList |
| Python | rarely used | list |
| C | int marks[5] | malloc + realloc, by hand |
| JavaScript | typed arrays like Int32Array | Array |
| Best when | The size is known and won't change | The size is unknown or keeps changing |
5. Jagged Arrays
A jagged array is a 2D array where the rows have different lengths. Imagine recording the scores of three cricket players, where each one batted a different number of innings:
player 0: | 10 | 20 |
player 1: | 30 | 40 | 50 |
player 2: | 60 |
public class Main {
public static void main(String[] args) {
// each row can have a different length
int[][] scores = {
{10, 20},
{30, 40, 50},
{60}
};
System.out.println(scores[1].length); // 3
System.out.println(scores[2][0]); // 60
}
}When looping through a jagged array, always use each row's own length in the inner loop, never a fixed number, or you will run past the end of the shorter rows.
Quick Summary
| Type | What it is | Example use |
|---|---|---|
| 1D array | A single row | Marks of one student |
| 2D array | A grid of rows and columns | A class's marks in every subject |
| Multi-dimensional | A grid of grids | Marks across several classes |
| Static array | Fixed size | Days of the week |
| Dynamic array | Grows as needed | A list of new admissions |
| Jagged array | Rows of different lengths | Innings per cricket player |
In the next lesson, we will put arrays to work by sorting them: why sorting matters, and the different kinds of sorting algorithms.
Remember This
If you remember only one thing from this lesson, remember:
A 2D array is a grid reached with [row][column], a static array has a fixed size, and a dynamic array grows by copying itself into a bigger space when it fills up.