• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting an arrayList  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you format an arraylist?

Mine looks like this:


But I want it to look like: I'd prefer if the '[' , '<>' and ',' were not on them also but I'm not too bothered about that bit.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were using a Java version issued more recently than the Great War, you could use a StringJoiner object.

What about a StringBuilder?
Start it with "[" then iterate the list and append each element. Then append the appropriate line end String. You can append several things at a time. I am not sure, but I think that is called method chaining.
joiner.append(element).append(LINE_END);
At the end, reduce the length by LINE_END.length() and append "]"
public static final String LINE_END = System.getProperty("line.separator");
Avoid \n and \r unless you are told specifically you want CR and LF.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayLists (and collection and arrays in general) do not have an innate formatting - how did this output come about?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ulf I did System.out.println(arrayList); and it printed in this very long line!

@Campbell I think I'm forever going to be stuck with a version of Java dating back to the great war! I'll have a look at string builder thank you
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did System.out.println(arrayList); and it printed in this very long line!

That's what I suspected. This causes the object's toString() method to be called, and you have no control over what that prints out (not unless you write your own List and String classes, and that is likely way beyond what you're prepared to do for this).

The alternative is to write a method that prints the contents of the list exactly as you would like to see it, along the lines of what Campbell suggested.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you were using a Java version issued more recently than the Great War...


To be fair, more like WW2. I think it's the version used for Colossus.
The Great War would have been Ada which was, of course, the language used by Babbage for his Difference Engine.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote: . . . I think it's the version used for Colossus. . . .
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dave! hahaha I think you very well may have a good point there!

I'll forever be stuck with Java 6!
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've formatted it but it has left all the commas in:



Given the fact I didn't put them in how can I take them out?

*Edit* This fixed it
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ArrayList version of toString automatically adds commas.
I would suggest it is not a good idea to put prints (or += with Strings) inside a loop, because of performance problems. Each print call takes a few milliseconds whereas it probably take a few microseconds to put all that text together with a StringBuilder. You might not notice anything until you are printing millions of elements, but get into good habits from the beginning. Similarly for +=You won't notice anything until j is in 5 figures (decimal) but as j gets bigger performance gets slower; for 100000 you are looking at tens of seconds as against tens of milliseconds with a StringBuilder.I still think the best solution is what somebody posted last week minus the square brackets.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it Campbell thankyou - I actually found a post that was on here off a few years back and it worked like a charm! I wish I hadn't posted these questions now as I figured it out not too long after posting and I want to post a thread now to query if my logic is correct but I have too many posts!
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jelle Klap thank you I forgot I had done that post to be honest with you - My issue is solved now though
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:I fixed it Campbell thankyou . . . I have too many posts!
Yes, I saw the old thread where you found your solution.
There is no such thing as too many posts. But after 200 you are required to buy me a pint of beer per post
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!