Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to arraylist <Character> in java

How to convert a String without separator to an ArrayList<Character>.

My String is like this:

String str = "abcd..." 

I know one way of doing this is converting the String to char[] first, and then convert the char [] to ArrayList <Character>.

Is there any better way to do this? like converting directly? Considering time and performance, because I am coding with a big database.

like image 435
Pan Long Avatar asked Mar 11 '13 05:03

Pan Long


People also ask

Can we convert string to ArrayList?

We can easily convert String to ArrayList in Java using the split() method and regular expression.

Can we convert a string to character array in java?

Java For Testers String str = "Tutorial"; Now, use the toCharArray() method to convert string to char array. char[] ch = str. toCharArray();

How do you create an ArrayList character in java?

Convert a String Into ArrayList Using the toCharArray() Method in Java. The toCharArray() method can be used on a string to convert it into a character array. We can then iterate over this character array and add each character to the ArrayList .


1 Answers

You need to add it like this.

String str = "abcd..."; ArrayList<Character> chars = new ArrayList<Character>(); for (char c : str.toCharArray()) {   chars.add(c); } 
like image 136
squiguy Avatar answered Oct 06 '22 00:10

squiguy