Hi.
I am tired using double loop to print 2d-array on a console.
Somebody know a ready-to-use-class in java api that print 2d array to String?
Something similar to Arrays.toString-method.
Printable View
Hi.
I am tired using double loop to print 2d-array on a console.
Somebody know a ready-to-use-class in java api that print 2d array to String?
Something similar to Arrays.toString-method.
I know of no simple methods that fit this bill. The only solution I can think of for simplifying the calling of a toString method on a 2-D array would be to create a wrapper class that held the 2-D array and create a toString override method that internally would use nested for-loops and a StringBuilder to build the String you desire.
Fubaable's recommendation is good, but even without that you don't need nested loops. Just call Arrays.toString in a single loop.dbCode:int[][] ints = {{1,2},{33,44},{555,666}};
for (int[] is : ints) {
System.out.println(Arrays.toString(is));
}
Darryl's suggestion is a good one, but if you look at the code for Arrays.toString, you'll see that it uses a StringBuilder and a for-loop, and so Darryl's example still uses at its core nested for-loops.
OK, so I cheated a little on this post. ;)
thank you darryl. your code looks beautifull =)