Thursday, December 4, 2014

Multi Dimensional Array

With the discussions on Array, here comes the part where we can concentrate on how to deal things in tabular format. This post will be more on organizing data, manipulating the data in a table and retrieving the data from the table.

To get the concept, let's start with a table of students' marks obtained in different subjects,







PhysicsMathematicsComputers
Palash Kanti Kundu607560
Suman Kundu659065
Totan Das Kundu657065

This is easy to deal right ?

You can easily tell that Totan has scored 65 in Physics, 70 in Mathematics, Suman has scored 90 in Mathematics and so on...

Now, that's the concept behind multi dimensional arrays.
So, let's start with the definition,

Multi Dimensional Array: An array of arrays.

Array of Arrays ?
Right, array of arrays. So, each index of a multi dimensional array contains another array. The following diagram shows the concept,


So, each index of the multi-dimensional array points to an array.

Now, let's see, how we define multi-dimensional arrays in Java,

We use the following 

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

This code particularly defines that multiDimensionalArray is an array of arrays which has the length 3 and each index of this array will contain an int array of length 4. In other words, it defines, multiDimensionalArray  is a table of 3 rows and 4 columns.

Now let's add some value to this array.

  multiDimensionalArray[0][0] = 1;
  multiDimensionalArray[0][1] = 2;
  multiDimensionalArray[0][2] = 3;
  multiDimensionalArray[0][3] = 4;
  
  multiDimensionalArray[1][0] = 5;
  multiDimensionalArray[1][1] = 6;
  multiDimensionalArray[1][2] = 7;
  multiDimensionalArray[1][3] = 8;
  
  multiDimensionalArray[2][0] = 9;
  multiDimensionalArray[2][1] = 10;
  multiDimensionalArray[2][2] = 11;
  multiDimensionalArray[2][3] = 12;

Now, you can see, we are using two pairs of square braces instead of one.

But Why ?
Well, I won't answer this. You have to find out why we have to use two square braces. If you are not getting the point, start reading from the first word once again, look the diagram very closely. You will surely understand...











Still you could not figure it out ?

Well, no issues, I am telling it. A multi dimensional array is an array of an arrays. So, to get into a correct place, you have to first find out the index of the multi dimensional array itself and then you have to find out the index in the sub-array.
Now let's modify the diagram once again and find out the index positions in the array instead of the values,


So, you can see that the original array is having 3 arrays of 4 values, So, all the 3 indexes are having an array of 4 values.

Now, if I take out the value from index 0 of the original array, we'll get an array of 4 values. Similar goes true for index 1 and index 2.

Now if I take out the index 0 of the sub-array of the original array at index 0, we'll get an int value.
That's why there are two indexes, the first one for the index of the original array and the second one for the index of the sub-array.

So, that was the part where we have added some value to the array. Now, we'll be checking how we can read the values from it.

Now, we will use our knowledge of loops and we'll print all the values in the array using loops,


  for (int i = 0; i < multiDimensionalArray.length; i++) {
   for (int j = 0; j < multiDimensionalArray[i].length; j++) {
    System.out.println(multiDimensionalArray[i][j]);
   }
  }

Well the whole source code looks like the following,

/*
 * Copyright 2014-2016 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Source code for detailing Multi Dimansional Array.
 * 
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Jan 17, 2016
 */
public class MultiDimesionalArray {

 /**
  * @param args
  */
 public static void main(String[] args) {
  int[][] multiDimensionalArray = new int[3][4];

  multiDimensionalArray[0][0] = 1;
  multiDimensionalArray[0][1] = 2;
  multiDimensionalArray[0][2] = 3;
  multiDimensionalArray[0][3] = 4;

  multiDimensionalArray[1][0] = 5;
  multiDimensionalArray[1][1] = 6;
  multiDimensionalArray[1][2] = 7;
  multiDimensionalArray[1][3] = 8;

  multiDimensionalArray[2][0] = 9;
  multiDimensionalArray[2][1] = 10;
  multiDimensionalArray[2][2] = 11;
  multiDimensionalArray[2][3] = 12;

  for (int i = 0; i < multiDimensionalArray.length; i++) {
   for (int j = 0; j < multiDimensionalArray[i].length; j++) {
    System.out.println(multiDimensionalArray[i][j]);
   }
  }
 }

}

And when run, this will print the values as follows,

1
2
3
4
5
6
7
8
9
10
11
12

Well, we could have used loops to fill the array and can change the output to look like a table. Well, let's do that in a new program.

/*
 * Copyright 2014-2016 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Source code for detailing Multi Dimansional Array.
 * 
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Jan 17, 2016
 */
public class ModifiedMultiDimesionalArray {

 /**
  * @param args
  */
 public static void main(String[] args) {
  int[][] multiDimensionalArray = new int[3][4];

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

  for (int i = 0; i < multiDimensionalArray.length; i++) {
   for (int j = 0; j < multiDimensionalArray[i].length; j++) {
    System.out.print(multiDimensionalArray[i][j] + "\t");
   }
   System.out.println();
  }
 }
}

And the output is,

1 2 3 4 
5 6 7 8 
9 10 11 12 

So, we can see that, using loops, we can work with multi-dimensional arrays easily. So, we've so far seen arrays, two dimensional arrays. In our next post, we'll be looking forward for arrays with more than two dimensions.

Source Code Distribution

Although this is not a good practice, you can download the source code. You can use them to cross check you program.


Prev     Next

No comments:

Post a Comment