Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 03:14 PM
Member
 
Join Date: Nov 2008
Posts: 19
Rep Power: 0
Tamu is on a distinguished road
Default simplest way to make 2d array to string
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 03:18 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,190
Rep Power: 5
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 07:06 PM
Senior Member
 
Join Date: Sep 2008
Posts: 607
Rep Power: 1
Darryl.Burke is on a distinguished road
Default
Fubaable's recommendation is good, but even without that you don't need nested loops. Just call Arrays.toString in a single loop.
Code:
int[][] ints = {{1,2},{33,44},{555,666}};
for (int[] is : ints) {
   System.out.println(Arrays.toString(is));
}
db
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2008, 07:11 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,190
Rep Power: 5
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-25-2008, 04:50 PM
Member
 
Join Date: Nov 2008
Posts: 19
Rep Power: 0
Tamu is on a distinguished road
Default
thank you darryl. your code looks beautifull =)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make Java see a string as html matpj Java Applets 4 09-26-2008 03:40 AM
how do i make a string return a number? pjr5043 New To Java 6 09-15-2008 04:56 AM
Simplest ApplicationWindow Example Java Tip SWT 0 07-02-2008 08:12 PM
make a variable name from a string? Kinnikinnick New To Java 3 11-13-2007 03:54 PM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 10:52 AM


All times are GMT +2. The time now is 10:16 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org