Two Dimensional Arrays
This lesson will briefly explain what Two Dimensional Arrays are and how they are indexed.
Introduction
Arrays can also store references as values. These references can point to anything. They can be a reference to an object or any other data structure. Before we move forward, let’s briefly discuss what pointers are:
-
References are used to explicitly store memory locations that hold a value or an object.
-
Any time you build an object in Java, you basically create a reference to that object.
Two Dimensional Arrays
A Two Dimensional Array is an array of references that holds references to other arrays. These arrays are preferably used if you want to put together data items in a table or matrix-like structure. Matrices are widely used in the field of Game Development, where you are required to store and update the location of the player at each second.
Take a look at the figure below to get a good understanding of what a Two Dimensional Array looks like:
Explanation: If we take a look at the structure of the Two Dimensional Array, we get the idea that a Two Dimensional Array is basically an array of One Dimensional Array; in other words, every element points to the first element of a different One Dimensional Array.
It is important to note that in 2D arrays, all values must have the same data type. This means that you can’t store an array of integers next to an array of strings and vice versa. For example, if one array is declared of type int
, then its pointer can’t point to the string
type array. Each element must be of the same data type.
Initialization
Initialization in Two Dimensional Arrays is done using two values for the number of Rows and Columns. Look at the following code to see how 2D array is declared and initialized in Java:
class TwoDArray {public static void main( String args[] ) {int rows = 3;int columns = 4;int [][] my2DArray;my2DArray = new int[rows][columns];System.out.println("You have successfully created a 2DArray");}}
Explanation: Let’s take a look at the statement line-by-line and see what it means.
- At line numbers 4 and 5, we declare variables of type
int
that stores the total number of rows and columns in the array we are about to declare. - The line number 7 creates a reference to store a 2D array. Since the
int
data type is specified in the beginning, this means that we can only store integer values in the array that we are going to initialize in the next line. - Line number 8 contains a keyword
new
. This keyword is used whenever you want to create a new object on the heap. Not only that, but it will also return a reference to that object. This reference is then assigned to themy2DArray
variable. - The size of the array is 3 by 4 (values assigned to
rows
andcolumns
variable), which means this Two Dimensional Array can hold three One-Dimensional Arrays, having four elements in each.
Adding Elements in 2-D Arrays
Now that we have learned how to declare a 2D array, let’s add values to the cells! See the following code:
class TwoDArray {public static void main( String args[] ) {int [][] my2DArray = new int[3][4];// add "10" at Row 0 and Column 1my2DArray[0][1] = 10;System.out.println(my2DArray[0][1]);}}
Can you look at the following code and point out what’s wrong?
class TwoDArray {public static void main( String args[] ) {int[][] myArray = new int[10][] ;for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){myArray[i][j] = i + j;System.out.println(myArray[i][j]);}}}}
The code given above will result in a NullPointerException
. The reason is that in line 4, we only allocate memory for the rows. You need to allocate memory for the columns as well.
Have a look at the following code that we have fixed for you!
class TwoDArray {public static void main( String args[] ) {int[][] myArray = new int[10][] ;for (int i = 0; i < 10; i++){myArray[i] = new int [10];for (int j = 0; j < 10; j++){myArray[i][j] = i + j;System.out.println(myArray[i][j]);}}}}
Example 1: Student’s Records #
Let’s say there’s a class of 30 students, and we want to store their grades for each of their courses (six courses in total). Is there a way we can map all this information using the knowledge we have covered so far? Yes, we can!
All we need to do is to keep the first column(i.e., Column 0), fixed for students, and the rest of the columns to store grades. We will have to follow a specific order to make sure the scores and students don’t get mixed up. Each element of the first column will hold a reference to an array containing marks of the student. So, there will be 30 rows and 6 columns. Now we can calculate the student’s percentage, class average, and the student’s position.
Three Dimensional Arrays
Just like Two Dimensional Arrays, we can also design Three Dimensional Arrays. These arrays are an extension to 2D Arrays but are slightly more complex as they have one additional feature. Now the values in the cell will also hold a reference to some object, any primitive type value, or any other data structure.
You can use 3D Arrays if you want to assemble your data in a “cube-like” structure. These arrays are indexed by three variables, one for each dimension.