Learn-dsa..in 30 days!



























Matrix Operations

Initializing the Matrix.

Following syntax can be used to create array based in matrix in java. The matrix is initialized to hold 12 integers, stored in 3 rows and 4 columns. Currently all values of the matrix are set to 0.

int[][] intArray= new int[3][4];

Please note that once the size of matrix is initialized it cannot be changed. If we want to set the value of some/all the positions/indexes of above array, we can use code as shown below. This code sets values in all the elements in first row of intArray:

intArray[0][0]=63;
intArray[0][1]=51;
intArray[0][2]=99;
intArray[0][3]=48;

Another way to initialize matrix of 3 rows and 4 columns is shown below. Each row has 4 column elements and the value at all positions/indexes has been initialized to the values in the curly brackets.

int[] intArray = {	{1,2,3,4},
					{5,6,7,8},
					{9,10,11,12} 
				  }

Please note that array indices for each row and column start from 0 and go up to one less that length of the row or column. Matrices of other java data types can also be created. Below, matrices of type float and String have been created. If you have a custom created class called Invoice, you can also create and matrix that holds objects of type Invoice as shown below

String[][] strArray=new String[10][4];
float[][] floatArray=new float[5][8];
Invoice[][] invArray=new Invoice[100][5];

Length of the matrix rows and columns.

The length of the matrix rows and columns be accessed using the length attribute as shown below:

int intArrayRowLength= intArray.length;
int intArrayColLength= intArray[0].length;

Accessing the Matrix elements.

To access particular element in the matrix we can simply use its index. Below we are printing the value at third row, 2nd column:

int[] intArray = {	{1,2,3,4},
					{5,6,7,8},
					{9,10,11,12} 
				  };
System.out.println(intArray[2][1]);

To iterate through all the matrix elements, we can use nested For loops. Nested For loop example is shown below. The outer loop iterates through all rows and the nested for loop iterated through all the columns. We could have used while loops also if needed.

for (rIdx=0; rIdx < intArray.length; rIdx++) {
for (cIdx=0; cIdx < intArray[rIdx].length; cIdx++) {
		System.out.print(intArray[rIdx][cIdx]);
	}
	System.out.print("\n");
} 

Jagged Arrays.

A two-dimensional array is a Jagged Array where the number of columns in the different rows in the two- dimensional array is not fixed. Below is a jagged array with 3 columns in first row and 4 columns in second row:

int[][] jagArray= new int[2][];
jagArray[0]=new int[3];
jagArray[1]=new int[4];

We need to take care that the all rows of Jagged Array may not be of same length, while doing operations like initialization, access, update etc.

3 or more dimensional Arrays.

We can declare and use arrays of more than 3 dimensions as well:

int[][][] 3dArray= new int[5][5][5];

Individual elements can be accessed by using 3 indices:

3dArray[0][0][1]=15;

We can iterate through all elements of 3d array by using 3 nested for loops. Similar to 3d arrays, arrays of more dimensions can also be created.