Search code examples
javaarrayliststringbuffer

Array List Output


I am currently using an Iterator to output an array list but I want to output it all on one line. I think my best bet would be to use a stringbuffer but does anyone have a more effective method?

My current method of output is this:

Iterator itr = cards.iterator();
while(itr.hasNext()){
    System.out.println(itr.next());
    System.out.println(); 
}

Went with this Not efficient at all but its all I understand at the moment:

 Iterator itr = cards.iterator();
 String str = "";
    while(itr.hasNext()){
        str += (itr.next() + ", ");
    }
    return str;

Solution

  • Use System.out.print() instead of System.out.println().

    See PrintStream