Converting ArrayList of Characters to a String?

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();
}

Setting the capacity of the StringBuilder to the list size is an important optimization. If you don’t do this, some of the append calls may trigger an internal resize of the builder.

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.

Leave a Comment