Online Test Banks
Score higher
See Online Test Banks
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Java: Use Arrays with Two Dimensions or More

In Java, the elements of an array can be any type of object you want, including another array. This is called a two-dimensional array — or (sometimes) an array of arrays.

Two-dimensional arrays

To declare a two-dimensional array, you simply list two sets of empty brackets, like this:

int numbers[][];

Here, numbers is a two-dimensional array of type int. To put it another way, numbers is an array of int arrays.

To create the array, you use the new keyword and provide lengths for each set of brackets, as in this example:

numbers = new int[10][10];

Here, the first dimension specifies that the numbers array has 10 elements. The second dimension specifies that each of those elements is itself an array with 10 elements.

To access the elements of a two-dimensional array, you use two indexes. For example:

int[5][7] = 23853;

Often, nested for loops are used to process the elements of a two-dimensional array, as in this example:

for (int x = 0; x < 10; x++) 
    for (int y = 0; y < 10; y++) 
        numbers[x][y] = (int)(Math.random() * 100) + 1

You can use an array initializer with a two-dimensional array, as in this example:

string members[][] = 
      {"Larry", "Curly", "Moe" },
      {"Manny", "Moe", "Jack"},
      {"Huey", "Dewey", "Louie"}
      {25483.0, 22943.0, 38274.0, 33294.0},    // 2005
      {24872.0, 23049.0, 39002.0, 36888.0},    // 2006
      {28492.0, 23784.0, 42374.0, 39573.0},    // 2007
      {31932.0, 23732.0, 42943.0, 41734.0} };  // 2008

When you create an array with an expression — such as new int[5][3] — you’re specifying that each element of the main array is actually an array of type int with three elements. Java, however, lets you create two-dimensional arrays in which the length of each element of the main array is different. Sometimes, this is called a jagged array because the array doesn’t form a nice rectangle. Instead, its edges are jagged.

Arrays with more than two dimensions

Java doesn’t limit you to two-dimensional arrays. Arrays can be nested within arrays to as many levels as your program needs. To declare an array with more than two dimensions, you just specify as many sets of empty brackets as you need. For example:

int[][][] threeD = new int[3][3][3];

Here, a three-dimensional array is created, with each dimension having three elements. You can think of this array as a cube. Each element requires three indexes to access.

You can access an element in a multidimensional array by specifying as many indexes as the array needs. For example:

threeD[0][1][2] = 100;

This statement sets element 2 in column 1 of row 0 to 100.

You can nest initializers as deep as necessary, too. For example:

int[][][] threeD = 
    {  { {1,   2,  3}, { 4,  5,  6}, { 7,  8,  9} },
       { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
       { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };

Here, a three-dimensional array is initialized with the numbers 1 through 27.

You can also use multiple nested if statements to process an array with three or more dimensions. Here’s another way to initialize a three-dimensional array with the numbers 1 to 27:

int[][][] threeD2 = new int[3][3][3];
int value = 1;
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++)
            threeD2[i][j][k] = value++;
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win $500. Easy.