This discussion is archived
1 2 3 Previous Next 35 Replies Latest reply: Apr 1, 2011 4:13 PM by jschellSomeoneStoleMyAlias RSS

Print an ArrayList in different format?

840446 Newbie
Currently Being Moderated
Is there any easy tricks to printing an ArrayList in a different format? Or do I have to make a for loop to iterate through the List?

Output:
[
Motherboard, Manufacturer = asus, Price = 50.0, Order Number = 1, 
Motherboard, Manufacturer = hp, Price = 50.0, Order Number = 1, 
Motherboard, Manufacturer = sony, Price = 50.0, Order Number = 1, 
RAM, Manufacturer = Rammy, Price = 50.0, Order Number = 1, 
RAM, Manufacturer = atari, Price = 50.0, Order Number = 1]
I would like to remove the square brackets and also the commas and line everything up in columns. Any easy way to do that? I probably have to make a for loop right?
  • 1. Re: Print an ArrayList in different format?
    Kayaman Guru
    Currently Being Moderated
    SquareBox wrote:
    Is there any easy tricks to printing an ArrayList in a different format? Or do I have to make a for loop to iterate through the List?
    For loop is a pretty easy trick. You can do that in two lines of code. I don't think you can get a lot easier than that.
  • 2. Re: Print an ArrayList in different format?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    SquareBox wrote:
    Is there any easy tricks to printing an ArrayList in a different format? Or do I have to make a for loop to iterate through the List?
    I would like to remove the square brackets and also the commas and line everything up in columns. Any easy way to do that? I probably have to make a for loop right?
    arrayList.toString.replaceAll("[\\[\\],]","");
    To line up in columns I'd sugest to alter the toString() method of the Objects in the list and replace the single space with a tab. Ofcause the better way was to display the List's content in a JTable....

    bye
    TPD
  • 3. Re: Print an ArrayList in different format?
    796440 Guru
    Currently Being Moderated
    T.PD wrote:
    SquareBox wrote:
    Is there any easy tricks to printing an ArrayList in a different format? Or do I have to make a for loop to iterate through the List?
    I would like to remove the square brackets and also the commas and line everything up in columns. Any easy way to do that? I probably have to make a for loop right?
    arrayList.toString.replaceAll("[\\[\\],]","");
    Ick. I wouldn't want to rely on the specific format of a class's toString(). Just go with the loop.
  • 4. Re: Print an ArrayList in different format?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    And won't the output look great with all the commas removed, not to mention any square brackets that may appear as part of the list's contents.

    db
  • 5. Re: Print an ArrayList in different format?
    Kayaman Guru
    Currently Being Moderated
    Well, he did say he wanted an "easy trick". Why bother with correct programming when you can do oneliners with replaceAll...
  • 6. Re: Print an ArrayList in different format?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Darryl Burke wrote:
    And won't the output look great with all the commas removed, not to mention any square brackets that may appear as part of the list's contents.
    I cannot imaging why one would print out something unstructured just pritty looking to standard out anyway...

    There are a lot of solutions and framworks to produce well structured CSV or XML output, but it seems the OP didn't want that.

    bye
    TPD
  • 7. Re: Print an ArrayList in different format?
    YoungWinston Expert
    Currently Being Moderated
    SquareBox wrote:
    I would like to remove the square brackets and also the commas and line everything up in columns. Any easy way to do that? I probably have to make a for loop right?
    Probably, although this suggests to me that you're planning on changing toString(). My advice: Don't.

    toString() is there to provide a human-readable representation of an object in String form, usually for debugging or logging; it is NOT generally meant for reports or other formatted output. By overriding a perfectly good working method, you're just removing this capability.

    My suggestion: write a separate method (format() ?) to output your object in "pretty" form.

    Winston
  • 8. Re: Print an ArrayList in different format?
    796440 Guru
    Currently Being Moderated
    T.PD wrote:
    Darryl Burke wrote:
    And won't the output look great with all the commas removed, not to mention any square brackets that may appear as part of the list's contents.
    I cannot imaging why one would print out something unstructured just pritty looking to standard out anyway...

    There are a lot of solutions and framworks to produce well structured CSV or XML output, but it seems the OP didn't want that.

    bye
    TPD
    Erm?

    Is there a point here?
  • 9. Re: Print an ArrayList in different format?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    jverd wrote:
    Is there a point here?
    No.

    Since we don't know why the OP wants to output a pritty formatted string there is no chance to tell if there was a better solution.

    If the output was just for debuggig the replaceAll() solution is to go with. Debugging doesnt justify any more effort.

    If the OP is trying to create some kind of user interface we most likely sould tell him that there was something invented called a GUI...

    So: no I was just blubbering.

    bye
    TPD
  • 10. Re: Print an ArrayList in different format?
    Kayaman Guru
    Currently Being Moderated
    T.PD wrote:
    If the OP is trying to create some kind of user interface we most likely sould tell him that there was something invented called a GUI...
    You do realize that not all programs are meant to have a Graphical UI?
    If someone is asking how to display an image, you don't see anyone going "Hey! Don't you know there's a 3rd dimension these days? You should use JOGL to create a 3D model of your image and display that instead".

    Jumping from ArrayList display to Swing's JTable is not exactly a leap one should make lightly.
  • 11. Re: Print an ArrayList in different format?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Kayaman wrote:
    You do realize that not all programs are meant to have a Graphical UI?
    Yes.
    But the non graphical programms I created the last 10 years where some kind of filters, or not written in java.
    If someone is asking how to display an image, you don't see anyone going "Hey! Don't you know there's a 3rd dimension these days? You should use JOGL to create a 3D model of your image and display that instead".
    I'm not really sure if thats the right abstraction, but I see your point.
    My point is: in the year 2000+ one should not consider a non graphical user interface.
    The only one exception is some kind of filter that gets parameters on startup and outputs results when finnished.
    Edited by: T.PD on 31.03.2011 12:03
    And that kind of program should not output something human readable...
    /Edit

    >
    Jumping from ArrayList display to Swing's JTable is not exactly a leap one should make lightly.
    Yes, it needs some knowledge and experience. But when would be the best time to start with? Usually here and now.

    bye
    TPD
  • 12. Re: Print an ArrayList in different format?
    EJP Guru
    Currently Being Moderated
    If the OP is trying to create some kind of user interface
    We don't know what he's doing.
    we most likely sould tell him that there was something invented called a GUI...
    Unless he is creating a report, or outputting HTML, or XML, or PDF, or something that types to an ASR-33 ...
    the non graphical programs I created the last 10 years where some kind of filters, or not written in java.
    So?
  • 13. Re: Print an ArrayList in different format?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    EJP wrote:
    T.PD wrote:
    If the OP is trying to create some kind of user interface
    We don't know what he's doing.
    True. But we do know that he's happy with the answers given since he gave points...
    we most likely sould tell him that there was something invented called a GUI...
    Unless he is creating a report, or outputting HTML, or XML, or PDF, or something that types to an ASR-33 ...
    Yes.
    I think I honored that in my second post to this thread.
    But eaven that supports my point: There is no need to output something pritty for human eyes to stdout.

    bye
    TPD
  • 14. Re: Print an ArrayList in different format?
    800268 Expert
    Currently Being Moderated
    T.PD wrote:
    pritty for human eyes
    <nitpick> http://www.merriam-webster.com/dictionary/pretty
1 2 3 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points