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.
We can easily convert String to ArrayList in Java using the split() method and regular expression.
Java For Testers String str = "Tutorial"; Now, use the toCharArray() method to convert string to char array. char[] ch = str. toCharArray();
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 .
You need to add it like this.
String str = "abcd..."; ArrayList<Character> chars = new ArrayList<Character>(); for (char c : str.toCharArray()) { chars.add(c); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With