Results 1 to 5 of 5
  1. #1
    Tamu is offline Member
    Join Date
    Nov 2008
    Posts
    19
    Rep Power
    0

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Fubaable's recommendation is good, but even without that you don't need nested loops. Just call Arrays.toString in a single loop.
    Java Code:
    int[][] ints = {{1,2},{33,44},{555,666}};
    for (int[] is : ints) {
       System.out.println(Arrays.toString(is));
    }
    db

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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. ;)

  5. #5
    Tamu is offline Member
    Join Date
    Nov 2008
    Posts
    19
    Rep Power
    0

Similar Threads

  1. How to make Java see a string as html
    By matpj in forum Java Applets
    Replies: 4
    Last Post: 09-26-2008, 03:40 AM
  2. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 04:56 AM
  3. Simplest ApplicationWindow Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:12 PM
  4. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •