0

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?

3
  • Another Homework Question? Commented Feb 15, 2013 at 3:08
  • Do you know how to get a single char from a String? Commented Feb 15, 2013 at 3:08
  • I don't want to convert it to a Character ArrayList (I know how to do that easily), but rather a String ArrayList. None of the solutions so far convert to an ArrayList<String>, rather an ArrayList<Character>. Commented Feb 15, 2013 at 3:24

7 Answers 7

6

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));
}
Sign up to request clarification or add additional context in comments.

Comments

2

Loop and use String.charAt(i), to build the new list.

Comments

2

You can do it by using the split method and setting the delimiter as an empty string like this:

ArrayList<String> strings = new ArrayList<>(Arrays.asList(string.split("")));

Comments

1

First you have to define the arrayList and then iterate over the string and create a variable to hold the char at each point within the string. then you just use the add command. You will have to import the arraylist utility though.

String string = "AEIOU";
ArrayList<char> arrayList = new ArrayList<char>();

for (int i = 0; i < string.length; i++)
{
  char c = string.charAt(i);
  arrayList.add(c);
}

The import statement looks like : import java.util.ArrayList;

To set it as a string arrayList all you have to do is convert the char variables we gave you and convert it back to a string then add it: (And import the above statement)

ArrayList<String> arrayList = new ArrayList<String>();
String string = "AEIOU"

for (int i = 0; i < string.length; i++)
{
  char c = string.charAt(i);
  String answer = Character.toString(c);
  arrayList.add(answer);
}

2 Comments

Pretty straight forward to do it as a string array too. All you do is convert the char variables back into a string.
Thank you for not being condescending like some other members. I appreciate it.
-1

Since everyone is into doing homework this evening ...

String myString = "AEIOU";
List<String> myList = new ArrayList<String>(myString.length());
for (int i = 0; i < myString.length(); i++) {
    myList.add(String.valueOf(myString.charAt(i)));
}

Comments

-1

For a single line answer use the following code :

    Arrays.asList(string .toCharArray());

1 Comment

Nope, it returns List<char[]>
-1

Kotlin approach:

fun String.toArrayList(): ArrayList<String> {
    return ArrayList(this.split("").drop(1).dropLast(1))
}

var letterArray = "abcd".toArrayList()
print(letterArray) // arrayListOf("a", "b", "c", "d")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.