Two Dimensional Array in Java

Two dimensional array in java are just array of an array.

Let’s discuss about the two dimensional arrays in java. For example,

Two Dimensional Array in Java

I have this table that has six rows and two columns. The first column represents the EmpID (Employee ID) and the second column represents the EmpName (Employee Name).

We will write a program that initializes EmpID, EmpName and then prints it out,

Example: Two Dimensional Array in Java

Also Read – Array Copy Program in Java

public class TwoDimensionalArray
{
public static void main(String[] args)
{
String twoDimensional [][] =
{
{"1501201","Chris"},
{"1501202","Sam"},
{"1501203","Smith"},
{"1501204","Williams"},
{"1501205","Joe"},
{"1501206","Mark"}
};
for(int a = 0;a < twoDimensional.length;a++)
{
System.out.println(twoDimensional[a][0] + "\t" + twoDimensional[a][1]);
}
}
}

Output:

Two Dimensional Array in Java

Above example is a two dimensional array class. It has a main method. We are declaring the two dimensional array. As we know each dimension needs to be square brackets. We put two brackets to declare that it’s two dimensional and the name of the array is twoDimensional.

And we are separating each row by the comma. So that tells it’s the next row. Once the EmpID and EmpName gets initialized what we are going to do is we are going to have a loop that goes through each of the ids, names and then print. After running the program the output is as above.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *