How to convert an ArrayList<Character> to a String in Java?
The List.toString method returns it as [a,b,c] string – I want to get rid of the brackets (etcetera) and store it as abc.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can iterate through the list and create the string.
Setting the capacity of the
StringBuilderto the list size is an important optimization. If you don’t do this, some of theappendcalls 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.