0

Any inherit method to traverse a multidimensional array?

Just after this tutorial, I was wondering what would happen if I try to traverse a 2D array, say: int[][] arr = new int[3][2]; // Some codes to assign values to the array for (int i[]: arr) { System.out.println(i); } Although Eclipse did not return any error, the display returns 3 lines of strange symbols, each was like “[D@15db9742”. I understand that I can and I managed to “manually” traverse the elements in an order that I wish using nested loops without problem, but I am just wondering if any default syntax can actually traverse a multidimensional array (with user-defined sorting order, if possible). If the above code actually can traverse a multidimensional array, what went wrong such that those strange symbols showed up? Thanks!

10th Aug 2016, 9:19 AM
Ka Ho Hui
Ka Ho Hui - avatar
2 odpowiedzi
+ 1
I think your problem is that you are trying to print out an array not the content of an array. for this you whould need another loop.
10th Aug 2016, 9:40 AM
Eric Zeus
Eric Zeus - avatar
+ 1
No default syntax, transversing is done via for loops. As for your error, Eric had it right. You've got your array indexes being placed into another array and then trying to just print the array. You either get rid of [] in your for loop, or in the println you have to indicate which elements of the array you want to display. like println(i[0]) will print all the elements in the 0 index. Example, you have {{1, 7, 9}{2,9,6}) it'll print 1 and 2 since they are both the 0 index.
10th Aug 2016, 9:48 AM
James
James - avatar