Java [snippet] : Automatic loop Array in 1 or 2 or 3 dimension

I love PHP with print_r function, it is so useful, but in java, there’s no on function equals to it, so I must to make it manually. Here i have some test array in java :

String[] data1 = new String[] {"1","2","3","4","5"};
String[][] data2 = new String[][]
{
{"1","2","3","4","5"},
{"6","7","8","9","10"},
{"11","12","13","14","15"},
};
String[][][] data3 = new String[][][]
{
{
{"1","2","3","4","5"},
{"6","7","8","9","10"},
{"11","12","13","14","15"},
},
{
{"21","22","23","24","25"},
{"26","27","28","29","30"},
{"31","32","33","34","35"},
}
};

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

public static void ret_3d(String[][][] x){
for (int i = 0;i < x.length; i++){
System.out.println("I = "+i);
for (int j = 0;j < x[i].length ; j++){
for (int k = 0;k < x[i][j].length ; k++){
System.out.println("["+i+"]["+j+"]["+k+"] = "+x[i][j][k]);
}
System.out.println();
}
System.out.println("===================");
}
}

public static void ret_2d(String[][] x){
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+"] = "+x[i][j]);
}
System.out.println("===================");
}
}

public static void ret_1d(String[] x){
for (int i = 0;i < x.length; i++){
if (x[i] != null){
System.out.println("["+i+"] = "+x[i]);
}
}
}
 

How to use it? just use this command in your main section :

System.out.println("Loop 1 dimension array");
ret_1d(data1);
System.out.println("Loop 2 dimension array");
ret_2d(data2);
System.out.println("Loop 3 dimension array");
ret_3d(data3);

And here’s the output :

Loop 1 dimension array
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
Loop 2 dimension array
I = 0
[0][0] = 1
[0][1] = 2
[0][2] = 3
[0][3] = 4
[0][4] = 5
===================
I = 1
[1][0] = 6
[1][1] = 7
[1][2] = 8
[1][3] = 9
[1][4] = 10
===================
I = 2
[2][0] = 11
[2][1] = 12
[2][2] = 13
[2][3] = 14
[2][4] = 15
===================
Loop 3 dimension array
I = 0
[0][0][0] = 1
[0][0][1] = 2
[0][0][2] = 3
[0][0][3] = 4
[0][0][4] = 5

[0][1][0] = 6
[0][1][1] = 7
[0][1][2] = 8
[0][1][3] = 9
[0][1][4] = 10

[0][2][0] = 11
[0][2][1] = 12
[0][2][2] = 13
[0][2][3] = 14
[0][2][4] = 15

===================
I = 1
[1][0][0] = 21
[1][0][1] = 22
[1][0][2] = 23
[1][0][3] = 24
[1][0][4] = 25

[1][1][0] = 26
[1][1][1] = 27
[1][1][2] = 28
[1][1][3] = 29
[1][1][4] = 30

[1][2][0] = 31
[1][2][1] = 32
[1][2][2] = 33
[1][2][3] = 34
[1][2][4] = 35

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

Hope this simple snippet is useful for you 😀

Advertisements

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