reesjones reesjones - 27 days ago 8
Java Question

How to convert String to ArrayList<String>

Let's say I have this String -

string = "AEIOU";


and I wanted to convert it to an
ArrayList<String>
, where each character is it's own individual
String
within the
ArrayList<String>


[A, E, I, O, U]


EDIT: I do NOT want to convert to
ArrayList<Character>
(yes, that's very straightforward and I do pay attention in class), but I DO want to convert to an
ArrayList<String>
.

How would I do that?

mre mre
Answer

transform the String into a char array,

char[] cArray = "AEIOU".toCharArray();

and while you iterate over the array, transform each char into a String, and then add it to the list,

List<String> list = new ArrayList<String>(cArray.length);

for(char c : cArray){
    list.add(String.valueOf(c));
}
Comments