Java [snippet] : Array 3 dimension loop

Here i have an array in java

String[][][] x = new String[][][]{
{{"2","2012-04-25", "0"},{"2","2012-04-27", "7"}},
{{"3","2012-04-22", "2"},{"1","2012-04-26", "5"}},
{{"4","2012-04-24", "3"},{"3","2012-04-25", "4"}},
{{"1","2012-04-29", "4"},{"4","2012-04-29", "0"}}
};


And i want to loop everything of these array, so here’s the code :

for (int i = 0;i < x.length; i++){
System.out.println("I = "+i);
for (int j = 0;j < x[i].length ; j++){
System.out.println("["+i+"]["+j+"][0] = "+x[i][j][0]);
System.out.println("["+i+"]["+j+"][1] = "+x[i][j][1]);
System.out.println("["+i+"]["+j+"][2] = "+x[i][j][2]);
System.out.println();
}
System.out.println("===================");
}
 

And here’s the output :

 I = 0
[0][0][0] = 2
[0][0][1] = 2012-04-25
[0][0][2] = 0

[0][1][0] = 2
[0][1][1] = 2012-04-27
[0][1][2] = 7

===================
I = 1
[1][0][0] = 3
[1][0][1] = 2012-04-22
[1][0][2] = 2

[1][1][0] = 1
[1][1][1] = 2012-04-26
[1][1][2] = 5

===================
I = 2
[2][0][0] = 4
[2][0][1] = 2012-04-24
[2][0][2] = 3

[2][1][0] = 3
[2][1][1] = 2012-04-25
[2][1][2] = 4

===================
I = 3
[3][0][0] = 1
[3][0][1] = 2012-04-29
[3][0][2] = 4

[3][1][0] = 4
[3][1][1] = 2012-04-29
[3][1][2] = 0

===================
 

Hope this simple snippet is useful for you 😀

Advertisements

One thought on “Java [snippet] : Array 3 dimension loop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s