1D and 2D Arrays
In Java, a 1D array (one-dimensional array) is the simplest way to store multiple values of the same type in a single variable.
Instead of declaring five separate variables for five test scores, you can store them all in a single array.
A 1D array works like a line of numbered boxes in memory. Each box has a unique index starting from zero.
For example, an array of size 5 has valid indices 0 to 4.
When you declare int[] scores = new int[5];
, Java sets aside enough space for five integers, all initialized to zero by default.
Arrays are fixed in length. Once you create an array of length 5, you cannot add a sixth element.
This makes arrays very efficient and predictable for small or medium-sized datasets where the size is known in advance.
Accessing and updating elements is very fast because the index maps directly to a memory address.
This is why arrays are still widely used in performance-critical code, even when Java Collections like ArrayList
exist.
One common error for beginners is the ArrayIndexOutOfBoundsException.
This happens when you try to read or write to an index that does not exist, like scores[5]
in a 5-element array.
Always remember that indices start at 0, so the last valid index is length - 1.
1D arrays are perfect for:
Storing exam scores,
Keeping track of sensor readings,
Holding simple, ordered lists like days of the week or monthly sales.
Key takeaway: If you know exactly how many items you need to store, a simple array is often the fastest, cleanest solution.
Mini Lab
A 2D array (two-dimensional array) expands the idea of a simple list into a grid or table.
In Java, a 2D array is actually an array of arrays.
This means each row is itself a 1D array that holds multiple columns.
A good way to visualize a 2D array is as a matrix or spreadsheet:
Rows = horizontal slices,
Columns = vertical slots.
You declare a 2D array like this: int[][] grid = new int[3][4];
This creates a grid with 3 rows and 4 columns.
By default, every element is set to zero for integers.
Accessing a cell uses two indices: the first for the row, the second for the column: grid[1][2]
means row 1, column 2.
A 2D array is powerful for:
Tables of data,
Seating charts,
Board games like chess,
Representing pixel grids for simple image operations.
One feature of Java 2D arrays is that they can be jagged: each row can have a different length.
This is less common but can be useful for irregular data, like storing a triangle of numbers.
When working with 2D arrays, nested loops are standard:
The outer loop goes over rows, the inner loop goes over columns.
Key pitfalls:
Mixing up rows and columns,
Forgetting that arrays are zero-indexed,
Not initializing the inner arrays when building custom jagged shapes.
Mastering 2D arrays prepares you for bigger data structures like matrices, grids, and more advanced algorithms later on.
Mini Lab
Arrays in Java are objects โ when you pass an array to a method, you are passing a reference, not a full copy.
This means any changes you make inside the method directly affect the original array outside.
This makes arrays powerful and efficient for:
Applying a filter,
Transforming data in place,
Performing batch calculations.
A good practice is to clearly document whether your method modifies the array or simply reads it.
Otherwise, unexpected changes can create hard-to-find bugs.
Also remember: you can return an array from a method too โ this is common for splitting data into parts or building new results.
Mini Lab