## Saturday, January 3, 2015

### How to use 2D two dimensional arrays in Java (Complete Overview)

Recently I've been working on a matrix caalculator app for android, then I thought of posting something about 2D arrays in Java.
As you all know an array is collection of data of similar type.
This is how we declare an array. (of type integers)
int[] integers;  //an array of type int
This is only half declaration It will only complete when you specify its length or its element.

int[] integers = new int    //an array of type int and length 5 or
int[] integers = new int[]{5, 54, 43, 48, 4};     //an integer array with elements  5, 54, 43,48 and 4

Or you can say its declaration is --
type[] = new type[size];   or  type[] = new type[]{elements};

And When we want to retrieve its elements we use the array name with the index of element such as -

int 3rdelement = integers;

Now notice i wrote 3rd element but wrote the index 2. that's because the index of array's element start from 0. So the first element's index is 0, and for second is 1 and 2 for 3rd and so on.
If you want to get all elements of array at once you can use for loops like this -

for(int element : integers){
//This loop loops for every element in the array once

for(int i = 0; i < integers.length - 1; i++){
this loop loops for every elements with i equals to the index of element

The simple array described above is one dimensional array because it stores elements in a line or in one row.
The two - dimensional array stores element in rows and columns just like a table (or a matrix). The only difference between a Table(or a matrix) and a 2D array is that the numbers of columns in a row must be equal in a Table but it can be irregular in a 2D array. For example -

But That doesn't mean you can't use a 2D array to store a matrix. Its upto you.
We declare a 2D array by using two square brackets like this '[][]'. ie.-

int[][] integers;   //a two dimensional array of integers

Now like the 1D array you need to specify either its size or element to declare it-

int[][] integers = new int       //a 2D array with 2 rows with 3 columns each
int[][] integers = new int[][]{ {1, 4, 5}, {8, 9, 6} };      //a 2d array with elements specified

The array defined can be written as-
1  4  5
8  9  6

Now to retrieve an element of 2D array we need to give its row index and column in square brackets- e.g.

int value = integers;

This will return the value of 3rd column of first row. Like the 1D array the index of first row or first column is 0. Therefore the first element in first column of first row can be accessed like this - integers.
Now there are two ways of retrieving all elements of a 2D matrix. First one is a for loop we used in 1D array -

for(int element : integers){
}

The other way of getting elements is by using a nested loop. Nested loop is made by starting a loop inside another loop like this. -

for(int i = 0; i < integers.length;  i++){
for(int j = 0; j < integers.length; j++){
//You can get the current element by using value of i and j like this 'integers[i][j]
}
}

Now one thing you will notice that i used integers.length in first loop and integers.length in second loop. The integers.length is the number of rows in a 2D array and integers.length return the number of columns in first row.
That is the major difference in the above for loop and nested loop, ie. The nested loop only works for rectangular 2D arrays or we can say for 2D arrays in which the number of columns are equal in each row.

To solve this problem we can set the second loop to loop for all the elements in current row like this -

for(int i = 0; i < integers.length;  i++){
for(int j =0; j< integers[i].length; j++){
}
}

Now that we know the basics of 2D array let's write an example Of 2D array-

This is just an simple example of transposing an array which means converting rows into column and columns into rows.

float[][] matrix = new float[][] {  {1.2, 1.3, 2.0}, {1, 5, 2.1}, {1.5, 5.1, 2.6}  };
float[][] transposedmatrix = new float;
for(int i = 0; i < integers.length;  i++){
for(int j = 0; j < integers.length; j++){
transposedmatrix[i][j] = matrix[j][i];
}
}
So  this was something about 2D arrays in Java. Feel free to comment. Actually I'm not able to post so frequently being a school student and need to prepare for exams.

#### 1 comment:

1. 