Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 05:14 PM
Member
 
Join Date: Nov 2008
Posts: 19
Tamu is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 11-22-2008, 05:18 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
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, 09:06 PM
Senior Member
 
Join Date: Sep 2008
Posts: 317
Darryl.Burke is on a distinguished road
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, 09:11 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
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, 06:50 PM
Member
 
Join Date: Nov 2008
Posts: 19
Tamu is on a distinguished road
thank you darryl. your code looks beautifull =)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 05:40 AM
how do i make a string return a number? pjr5043 New To Java 6 09-15-2008 06:56 AM
Simplest ApplicationWindow Example Java Tip SWT 0 07-02-2008 10:12 PM
make a variable name from a string? Kinnikinnick New To Java 3 11-13-2007 05: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 12:52 PM


All times are GMT +3. The time now is 10:11 AM.


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