Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting a String to an ArrayList

 
Alexander Cowie
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
System.out.print(s);
wordList.set(i, s);
i++;
}

thanks in advance.
 
Md Ibrahim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>();

YourArrayList.add(YourString);

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 know....do write back...
Have a nice day ...bye bye

By,
Md Ibrahim

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch

Your question doesnt have enough information
 
Alexander Cowie
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks man, i dont think i explained the problem very well so i will try to make it a little clearer.lol

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);
System.out.print(s);
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



Cheers,
Raj.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic