Converting ArrayList of Characters to a String?

前端 未结 11 2130
清歌不尽
清歌不尽 2020-12-05 13:39

How to convert an ArrayList to a String in Java? The toString method returns it as [a,b,c] string - I wa

11条回答
  •  抹茶落季
    2020-12-05 13:40

    You can iterate through the list and create the string.

    String getStringRepresentation(ArrayList 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.

提交回复
热议问题