phoenix phoenix - 2 months ago 7
Java Question

Converting ArrayList of Characters to a String?

In Java How do you convert an ArrayList = ['a','b','c'] to a String, the toString stores it as [a,b,c] I want to get rid of the brackets and store it as abc

Answer

You can iterate through the list and create the string.

String getStringRepresentation(ArrayList<Character> list)
{    
    StringBuilder builder = new StringBuilder(list.size());
    for(Character ch: list)
    {
        builder.append(ch);
    }
    return builder.toString();
}

As an aside, toString() returns a human-readable format of the ArrayList's contents. It is not worth the time to filter out the unnecessary characters from it. It's implementation could change tomorrow, and you will have to rewrite your filtering code.