1
0

Using Two Dimensional Array in Java

Since an array element can be an object reference and arrays are objects, array elements can themselves refer to other arrays. In Java, an array of array can be defined as follows:

<element-type> [][][]........[] <array-name>;

OR

<element-type> <array-name>[][][] ........[];

In fact, the sequence of brackets [][][]……..[] indicating the number of dimensions Arrays of arrays is also sometimes called multidimensional arrays.

The following declarations are equivalent:

int[][] MyArray;    // Two-Dimensonal Array
int[] MyArray[];    // Two-Dimensonal Array
int MyArray[][];    // Two-Dimensonal Array

The following is a combination of construction and declaration of a Two-Dimensional array.

int[][] MyArray = new int[5][6];    // 5x6 matrix on integer values

In the above declaration, MyArray constructs 5 elements, where each element is an array (or row) of 6 integer values. The concept of rows and columns is used to describe the dimensions of a two-dimensional array, often called-as a Matrix.

Multidimensional arrays can also be constructed and initialized explicitly using the array initializer block i.e.

float[][] Fee = {
    {2323.25f, 2451.23f, 2345.14f},
    {4323.25f, 4451.23f, 3345.14f},
    {7323.25f, 6451.23f, 4345.14f},
    {9323.25f, 8451.23f, 5345.14f},
};

Multidimensional arrays can be constructed in such a way so that every row contains different elements as:

int[][] MyArray = new int[4][]; // Specifies the no. of Rows
MyArray[0] = new int[2];    // First row has 2 elements
MyArray[1] = new int[4];    // Second row has 4 elements
MyArray[2] = new int[3];    // Third row has 3 elements
MyArray[3] = new int[5];    // Fourth row has 5 elements

Following is a two-dimensional array TwoDimensionArray program (To find the smallest element in an array):

Program to search the smallest element in a two-dimensional array.

class TwoDimensionArray
{
    public static void main(String[] st)
    {
        int[][] Array={{16, 7, 12},{9, 20, 8}, {8, 5, 10}}; 
        int min = Array[0][0];
        for(int i=0;i<Array.length; i++)
        {
            for(int j=0; j<Array[i].length; j++)
            {
                if(min > Array[i][j])
                    min=Array[i][j];
            }
        }
    System.out.println("Smallest element in given array: "+min);
    }
}

The output of the above program:

Smallest element in given array: 5

Program to add matrices (2-Dimensional arrays)

class Addition
{
    public static void main(String args[])
    {
        short i, j;
        int a[][] = new int[3][2];
        int b[][] = new int[3][2] = {{11,21},{43,42},{50,16}};
        int c[][] = new int[3][2] = {{1,2},{4,4},{7,6}};
        System.out.println("First Matrix: ");
        for(i=0; i<3; i++)
        {
            for(j=0; j<2; j++)
            {
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("Second Matrix: ");
        for(i=0; i<3; i++)
        {
            for(j=0; j<2; j++)
            {
                System.out.print(c[i][j]+" ");
            }
            System.out.print();
        }
        for(i=0; i<3;i++)
        {
            for(j=0; j<2; j++)
            {
                a[i][j] = b[i][j] + c[i][j];
            }
        }
        System.out.println("Addition of Above Matricies: ");
        for(i=0; i<3; i++)
        {
            for(j=0; j<2; j++)
            {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
}

The output of the above program:

First Matrix:
    11 21
    43 42
    50 16
Second Matrix:
    1 2
    4 4
    7 6
Addition of Above Matricies:
    12 23
    47 46
    57 22

Program to Transpose the matrix (2-Dimensional array)

class Transpose
{
    public static void main(String args[])
    {
        short i, j;
        int b[][] = new int[2][3];
        int c[][] = new int[3][2];
        b[0][0] = 1;
        b[0][1] = 2;
        b[0][2] = 3;
        b[1][0] = 4;
        b[1][1] = 5;
        b[1][2] = 6;
        System.out.println("Original Matrix: ");
        for(i=0; i<2; i++)
        {
            for(j=0; j<3; j++)
            {
                System.out.print(b[i][j]+" ");
                c[j][i] = b[i][j];      // find transpose
            }
            System.out.println();
        }
        System.out.println("Transpose of Above Matrix: ");
        for(i=0; i<3; i++)
        {
            for(j=0; j<2; j++)
            {
                System.out.print(c[i][j]+" ");
            }
                System.out.println();
        }
    }
}

The output of the above program:

Original Matrix: 
1 2 3 
4 5 6 
Transpose of Above Matrix: 
1 4 
2 5 
3 6 

Leave a Reply