Title: Arrays Multi-Dimensions
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Arrays Multi-Dimensions

1. char[][] ticTacToe = new char[3][3];
int[][]battleShip = new int[10][15];
int[][]testScores = new int[4][5];

2. You can also create your array as follows:
int[][]battleShip ;
...... Some code
battleShip = new int[10][15];
3. Of course you can also have:
double[][] table;
String[][] stringTable;
Person[][] personTable;
4. Consider the following code for filling in an array:


public class Test {


    public static void main(String[] args) {
        char[][] page = new char[30][100];

        // Hard Coded approach

        for (int row=0; row < 30; row++)
        {
              for (int col =0; col < 100; col++)
              {
                  page[row][col] = 'z';
              }
        }

        // Proper way

        for (int row=0; row < page.length; row++)
        {
              for (int col =0; col < page[row].length; col++)
              {
                  page[row][col] = 'z';
              }
        }

        // Works is almost all cases

        for (int row=0; row < page.length; row++)
        {
              for (int col =0; col < page[0].length; col++)
              {
                  page[row][col] = 'z';
              }
        }

    }

}



5. Note the equivalent declaration of testScores:


  public class Test {

    public static void main(String[] args) {

        // The normal way to create this is
        // int[][] testScores = new int[4][5];
        // But the following is closer to the way Java thinks.

        int[][] testScores = new int[4][];
        testScores[0] = new int[5];
        testScores[1] = new int[5];
        testScores[2] = new int[5];
        testScores[3] = new int[5];

        for (int student =0; student < testScores.length; student++)
        {
            for (int test=0; test < testScores[student].length; test++)
            {
                testScores[student][test] = 0;
            }
        }
    }

}


6. Although you will probably never need it, Java supports "Ragged Arrays":

public class Test {

    public static void main(String[] args) {

        // The normal way to create this is
        // int[][] testScores = new int[4][5];
        // But the following is closer to the way Java thinks.

        int[][] testScores = new int[4][];
        testScores[0] = new int[5];
        testScores[1] = new int[5];
        testScores[2] = new int[6];
        testScores[3] = new int[7];

        for (int student =0; student < testScores.length; student++)
        {
            for (int test=0; test < testScores[student].length; test++)
            {
                testScores[student][test] = 0;
            }
        }
    }

}



7. Multi-dimensional arrays can be passed as parameters:

public static void showMatrix(int[][] a)
    {
        for (int row=0; row < a.length; row++)
        {
            for (int col=0; col < a[row].length; col++)
            {
                System.out.print(a[row][col] + " ");
            }
            System.out.println();
        }
    }


8. You can also return multi-dimensional arrays

public static  double[][] corner (double[][] a, int size)
    {
        double[][] temp = new double[size][size];
        for (int row = 0; row < size; row++)
        {
            for (int col=0; col < size; col++)
            {
                temp[row][col] = a[row][col];
            }
        }

        return temp;
    }



Self Test Exercises:

1. What is the output produced by the following code?


    public class Test {

    public static void main(String[] args) {

        int[][] myArray = new int[4][4];
        for (int index1 = 0; index1 < myArray.length; index1++)
        {
            for (int index2 = 0; index2 < myArray[index1].length; index2++)
            {
                myArray[index1][index2] = index2;
            }
        }


        for (int index1 = 0; index1 < myArray.length; index1++)
        {
            for (int index2 = 0; index2 < myArray[index1].length; index2++)
            {
                System.out.print(myArray[index1][index2] + " ");
            }

            System.out.println();
        }
    }

}


Last Modified: June 2, 2014 4:50 PM