Two Dimensional Arrays in Java with Examples

0

In general, Array is a collection of the elements, where elements are of same data type. They are fixed in size. Array gives the random access of the elements of an array are index based and that is the main advantage of the array. But the biggest disadvantage is, its fixed size nature i.e. once Array declared, the size of the array can’t be an increase in run time.

Arrays in java Example:-

int myarr[] = {10,11,12,13};

  • It is one dimension array
  • Size of the array is 4
  • Index are 0,1,2,3
  • Value at index 0 is 10
  • Value at index 1 is 11
  • Value at index 2 is 12
  • Value at index 3 is 13
  • Try to access value at index greater than 3 or less than 0 will through run-time error( Array Index out of Bound)

In java array is an object and each has its own classes, i.e they are not simple variable like other language c/c++.

For example, for int type array has class name [I (capital I)

int myarr[] = {1,2,3,4,5};
System.out.println(myarr.getClass().getName());
char mychar[] = {1,2,3,4,5};
System.out.println(mychar.getClass().getName());
Output is
[I
[C

Two Dimensional Arrays in java comes into the flavor. These are known as

  1. Matrix
  2. Array of Array

Depending on the declaration it can be defined either as Matrix or Array of Array.

Take a scenario as given below.

Suppose we want to show in a school how many classes are there and each class how many students are reading.

Let’s assume that there is five class in a school. Class 1 has 1 students, class 2 has 2 students, class 3 has 3 students, class 4 has 4 students and class 5 has 5 students.

So in 2-D arrays java we can represent as

int [][] students = new int[5][5];                               // This is matrix format

Now we do some analysis in with respect to memory, as new int[5][5] will allocate 5*5*4 bytes of memory. But as classes have not the same number of students there is a lot of memory wastage.

For class 1 there are 4*4 bytes, waste of memory

For class 2 there are 3*4 bytes, waste of memory

For class 3 there are 2*4 bytes, waste of memory

For class 4 there are 1*4 bytes, waste of memory

So in total, we have 40 bytes, waste of memory. To save this much memory java has the second flavor known as an array of array.

Declaration of Array of Array in Java:-

if we consider above scenario than, First declare and define array for number of class which is 5

int [][] students = new int[5][];

You can observe that second ‘[]’ is empty, created one dimension array with size 5.

Now for each class we can define one dimension array like students[0] = new int[1]; // For class 1 we have one students so define one dimension array with size 1

students[0] = new int[1]; // For class 1 we have one students so define one dimension array with size 1

students[1] = new int[2]; // For class 2 we have one students so define one dimension array with size 2

students[2] = new int[3]; // For class 3 we have one students so define one dimension array with size 3

students[3] = new int[4]; // For class 4 we have one students so define one dimension array with size 4

students[4] = new int[5]; // For class 4 we have one students so define one dimension array with size 5

So by define array if array we saved 40 bytes, which were losing in Matrix format.

Now we will see a different type of declaration of java two dimension arrays.

  • int myarr[2][3] = new int[2][3]; // Compile Time Error

NEVER GIVE SIZE INFO OF ARRAY LEFT HAND SIDE

  • int [][] myarray = new int[2][]; // Valid declaration
  • int [][] myarray = new int [][3]; // Invalid Declaration
  • int [] myarray[]; // Valid syntax
  • int myarray[][]; // Valid syntax
  • int [][] myarray1, myarrat2; // valid syntax, myarray1 and myarray2 are java 2-D array
  • int [] myarray1[], myarray2; // Valid syntax, myarray1 is two D array and myarray2 is one D array
  • int [] arr1[], arr2[];       // Valid syntax, both are 2-D array java

As in java Arrays are object i.e they have class. Array class have a variable to get the information of size and that variable name is “length”.

2-D arrays in java Example:-

public class myClass
{
public static void main(String[] args)
{
int [][] students = new int[5][];
for (int i = 0; i <5 ; i++)
students[i] = new int[i+1];
for(int i = 0; i < 5 ; i++)
System.out.println("Size of students[" + i + "] is " + students[i].length);
}
}

Output :

Size of students[0] is 1

Size of students[1] is 2

Size of students[2] is 3

Size of students[3] is 4

Size of students[4] is 5

Example of Two D array java with initialization:-

double [][] salary = {
{1234.0},                         // First row has one element
{235.0, 2345.0};            // Second row has two element
{9999.0}                          // Third row has one element
}

Two dimensional array java Example:-

double [][] salary = new double[][] {
{1234.0},                         // First row has one element
{235.0, 2345.0};            // Second row has two element
{9999.0}                          // Third row has one element
}
5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply