Initializing Multidimensional Arrays

 

Introduction.  We have described a bit about multidimensional arrays in the last webpage.  Now we will discuss some about initializing them.  Since we have more dimensions to work with you should expect to see more options.

You can still initialize arrays making use of lists, but this can be done for the entire array at once or for selected parts.

  • Initializing completely making use of sets/lists should look like the following for two dimensional arrays.

dataType  arrayName[ ][ ] = {{list for row 0}, {list for row 1}, . . . {list for row n-1}}

where {list for row i} = {e0, e1, e2, ... ei} of the appropriate data type.  Thus, rows can each have a different number of elements.

 

  • In order to develop the contents of each row separately the developer is likely to have to do something like the following.

dataType  arrayName[ ][ ];

arrayName = new dataType[numberRows][ ];

arrayName[rowNumber][ ] = {e0, e1, e2, ... ei};

Then the developer can add the appropriate commands for initializing each row.

 

  • Something analogous to this can also be used to prepare a multidimensional array for initializing either within loops or from a GUI.

 

Now we will develop and discuss an example to illustrate these options.  You should call the following applet InitMultiDimArray.java.

 

import java.awt.Container;
import javax.swing.*;

public class InitMultiDimArray extends JApplet
{

JTextArea outputArea;

public void init( )
{

outputArea = new JTextArea( );
Container c = getContentPane( );
c.add(outputArea);

int array1 [][] = {{1,2,3},{4,5,6}};
int array2 [][] = {{1,2},{3},{4,5,6}};
int array3 [][];
array3 = new int [3][]; // array3 has 3 rows
array3[0] = new int[5]; // the first row has 5 columns
array3[1] = new int[2]; // the second row has 2 columns
array3[2] = new int[7]; // the third row has 7 columns

// actually initializing the values in array3
for (int i = 0; i < array3.length; i++)

for (int j = 0; j < array3[i].length; j++)

array3[i][j] = (i+1)*(j+1);

outputArea.setText("Values in array1 by row are\n");
buildOutput(array1);

outputArea.append("Values in array2 by row are\n");
buildOutput(array2);

outputArea.append("Values in array3 by row are\n");
outputArea.append("array3[ i ][ j ] = (i+1)(j+1)\n");
buildOutput(array3);

}

public void buildOutput(int a[][])
{

for (int i = 0; i < a.length; i++)
{

for (int j = 0; j < a[i].length; j++)

// display each element of the array
// while putting a space in between them

outputArea.append(a[i][j] + " ");

// add a new line for each new row
outputArea.append("\n");

}

outputArea.append("\n");

}

}

 

The HTML file follows and should be called InitMultiDimArray.html.

 

<html>
<applet code="InitMultiDimArray.class" width=200 height=300>
</applet>
</html>

 

Before we discuss the code, you should run the program to get the following output.

 

 

We will use our usual outline approach to discuss the code.
  • The imports bring in the necessary extra packages and classes
    • import java.awt.Container
      • for the container used for output
    • import javax.swing.*
      • for the swing classes and the JApplet in particular
  • Declare and define the overall inclusive class InitMultiDimArray
    • extends JApplet
  • Declare and define the init( ) method
    • instantiate a JTextArea called outputArea for output
    • instantiate a container called c for output
    • add the JTextArea to the container c
    • declare and initialize a new two dimensional integer array1 with a list
      • rows are each the same size
    • declare and initialize a new two dimensional integer array2 with a list
      • rows each have different size
    • declare a new two dimensional integer array3
    • declare the number of rows in array3
    • declare the number of columns in each of thee rows
    • use a for loop to go through the rows of array3
      • notice the use of array3.length to determine the number of loops based on  the number of rows
      • use a nested for loop to go through every column in each of these rows
        • notice the use of array3[row].length to determine the number of loops based on  the number of columns in each row
          • assign (i + 1)(j + 1) to the array3[ i ][ j ] element
    • append a descriptive phrase to the outputArea before displaying array1
    • use a user developed method called buildOutput(array1)
      • passing array1 to display array1 in the outputArea
    • append a descriptive phrase to the outputArea before displaying array2
    • use a user developed method called buildOutput(array2)
      • passing array1 to display array2 in the outputArea
    • append two descriptive phrases to the outputArea before displaying array3
    • use a user developed method called buildOutput(array3)
      • passing array1 to display array3 in the outputArea

       

 

  • declare and define the user defined method buildOuput( )
    • requires the array a[ ][ ] to be passed
    • returns nothing
    • use a for loop to go through the rows of a
      • notice the use of a.length to determine the number of loops based on  the number of rows in whatever is passed to the method
      • use a nested for loop to go through every column in each of these rows
        • notice the use of a[row].length to determine the number of loops based on  the number of columns in each row
          • append the a[ i ][ j ] element and a blank for each element in the row
          • append a newline character each time the row tracking loop goes to a new row
    • append a newline character just before completing append for output display for each time the method is invoked.

 

That's it!