File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes converting a String to an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting a String to an ArrayList" Watch "converting a String to an ArrayList" New topic

converting a String to an ArrayList

Alexander Cowie

Joined: Mar 25, 2010
Posts: 8
I am having some trouble getting a string into an array list, below is the code i have tried. Any advice on where i am going wrong would be great.

while(i != (word.length())){
s = word.charAt(i);
wordList.set(i, s);

thanks in advance.
Md Ibrahim

Joined: Mar 24, 2010
Posts: 14
Hi Alex,

I really dont know why you have so much difficulty in simply adding a String Object to an ArrayList.

You can do this very simply.

String YourString = "Hi this is your srting you wanted to add to list";
ArrayList<String> YourArrayList = new ArrayList<String>();


Try printing for your verification as System.out.println("Your verified List has>>"+YourArrayList.get(0))

Thats it ther you Go...............

(am sure you must be a college guy :-) because i use to be like you in my c-days)..

Hey buddy, If you were expecting any other approach then let me write back...
Have a nice day ...bye bye

Md Ibrahim

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to Javaranch

Your question doesnt have enough information
Alexander Cowie

Joined: Mar 25, 2010
Posts: 8
hey thanks man, i dont think i explained the problem very well so i will try to make it a little

i have a String Ex. "BLUE"

i want to place each character of the String at a different index of the ArrayList.

s = word.charAt(i);
by using the charAt() im getting the desired Char from the String

wordList.set(i, s);
but then when i try and set this to the ArrayList it doesnt work.
ive tried converting the value of s to a String but cant seem to do it?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445


Are you trying to insert each character in the String as a new element to the List? Try something like this:-

I agree. Here's the link:
subject: converting a String to an ArrayList
It's not a secret anymore!