header

Array of Arrays

Topics

Array of Arrays Fundamentals

Recall that an array is a data structure used to store multiple elements of the same type. If each element in an array is, itself, an array then the structure is referred to as an array of arrays. To help avoid confusion, I'll refer to each element in an array of arrays as a sub-array. I'll use the term primary array to refer to the array of arrays. In many programming languages, all sub-arrays must be of the same length. However, this is not required in Java. Here is an example of a primary array, named ArrOfArr, of length four:

Notice that, in Java, the sub-arrays can be of different lengths. Each sub-array is located by its index within the primary array. Each element within a sub-array would be located by its index within the sub-array (see below).

A Two-Dimensional Array

A two-dimensional array represents a rectangular grid (or table) of elements of the same type:

In Java, a two-dimensional array is represented by an array of arrays in which every sub-array is of the same length:

Declaring an Array of Arrays

Here is the syntax for declaring an array of arrays variable:

type[][] varId;

where type specifies what kind of values will be stored in the array of arrays and varId is the variable identifier. The pair of square brackets indicates that the identifier refers to an array of arrays rather than a single value or a simple array. Here are some example declarations:

int[][] intArrOfArr;      // An array of integer arrays
float[][] floatArrOfArr;  // An array of float arrays
String[][] strArrOfArr;   // An array of string arrays

Declaring an array of arrays does not allocate memory for the primary array or for any of the sub-arrays. Memory is allocated only when you create the primary array and the sub-arrays

Creating an Array of Arrays

The primary array is created using the new operator:

varId = new type[primarySize][];

Creating the primary array does not create any of the sub-arrays. The elements in the newly-created primary array are null references. For each element in the list, you must create a new array and assign it to the corresponding array element. For the first example at the top of this page you would write:

type[][] ArrOfArr;
ArrOfArr = new type[4];
varId[0] = new type[10];
varId[1] = new type[4];
varId[2] = new type[7];
varId[3] = new type[5];

If every sub-array is of the same length (a two-dimensional array), you can create both the primary and sub-arrays at the same time:

varId = new type[primarySize][subSize];

The primarySize is the number of rows and the subSize is the number of columns in the two-dimensional array. Here is how you would create a two-dimensional array of integers with 4 rows and 10 columns:

int[][] ArrOfArr;
ArrOfArr = new int[4][10];

It is not necessary to use all of the rows and/or columns in a two-dimensional array. Often a program maintains a variable to store the number of rows  that are actually used and another variable to store the number of columns that are actually used.

You can declare and create an array of arrays in a single statement as shown in these examples:

int[][] intArrOfArr = new int[4][];
float[][] floatArrOfArr = new float[4][10];

In the first example, only the primary array has been created. In the second example, both the primary and the sub-arrays have been created.

If an array of arrays contains objects (rather than primitive data), remember that creating the array of arrays does not allocate memory for the objects; just memory for the object references. The code fragment below creates a two-dimensional array of Strings and constructs all of the String elements.

// Create the two-dimensional table
String[][] strTable = new String[4][5];
// Construct all the String objects
for(row = 0; row < strTable.length; row++)
{
    for(col = 0; col < strTable[row].length; col++)
    {
       strTable[row][col] = new String();
    }
}

Note: strTable.length is the number of rows in the table and strTable[row].length is the number of columns in the specified row. The code to construct the String objects would work even if the rows had different numbers of columns.

Accessing an Element in an Array of Arrays

When accessing an element in an array, you must first specify the sub-array (a primary array index) and then the sub-array index:

varId[primaryIndex][subIndex]

In a two-dimensional array, the primary index corresponds to the row and the sub-array index corresponds to the column:

varId[row][column]

Getting the Length of an Array of Arrays

You can determine the length of the primary array or any of the sub-arrays by using the length data member:

ArrOfArr.length returns the length of the primary array.
ArrOfArr[0].length returns the length of the sub-array at index 0.

Initializing an Array of Arrays

By default, a newly created array of arrays will contain null values (i.e., zeroes). Java provides a shortcut way to declare, create, and initialize an array of arrays using the same syntax used to initialize a one-dimensional array. The elements appear in a comma-delimited list enclosed within braces. The difference is that each element of an array of arrays is, itself, an array. Consequently, each element (i.e., each sub-array) is a comma-delimited list enclosed by braces. Here are a couple of examples:

Code Result
int[][] ArrOfArr = {
                     { 0},
                     {10, 11},
                     {20, 21, 22}
                   }
int[][] ArrOfArr = {
                     { 0,  1,  2},
                     {10, 11, 12}
                   }

The same thing can be done using nested loops. The two code fragments below correspond to the two examples in the table above. In the first example, the sub-arrays are created and initialized within the row-counting loop. In the second example, all of the sub-arrays are the same size. Consequently, the entire two-dimensional array is created using the new operator. Only the initialization of the sub-arrays is done within the row-counting loop.

int row, col;     // Row counter, column counter
int[][] ArrOfArr; // Array of integer arrays

ArrOfArr = new int[3][];  // Create primary array

for(row = 0; row < ArrOfArr.length; row++)
{
    ArrOfArr[row] = new int[row + 1];
    for(col = 0; col < ArrOfArr[row].length; col++)
    {
        ArrOfArr[row][col] = 10*row + col;
    }
}

int row, col;     // Row counter, column counter
int[][] ArrOfArr; // Array of integer arrays

ArrOfArr = new int[2][3];  // Create two-dimensional array

for(row = 0; row < ArrOfArr.length; row++)
{
    for(col = 0; col < ArrOfArr[row].length; col++)
    {
        ArrOfArr[row][col] = 10*row + col;
    }
}