Magnus
Magnus

Reputation: 1442

How can I remove the [,] symbols from the result of ArrayList.toString()?

I have a method which returns an ArrayList in my Android app. Everything works great, but one thing annoys the heck out of me. When I print out the ArrayList in my TextView it looks like this:

[firstValue
, secondValue
, thirdValue
]

How can I remove [ and , from the output?

Upvotes: 2

Views: 243

Answers (2)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

You might want this:

public String formatList(List<?> list) {
    StringBuilder b = new StringBuilder();
    for(Object o : list) {
        b.append(o);
    }
    return b.toString();
}

The toString() method of the lists (and other collections) is mainly for debugging output.

Upvotes: 3

Marvo
Marvo

Reputation: 18133

This is coming from ArrayList's toString() method (or some underlying collection's toString()). If it's bugging you that much, extend ArrayList and override toString(). But because it's just debugging output, I'd personally try and get over your anxiety. ;)

Upvotes: 2

Related Questions