| Author |
converting a String to an ArrayList
|
Alexander Cowie
Greenhorn
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);
System.out.print(s);
wordList.set(i, s);
i++;
}
thanks in advance.
|
 |
Md Ibrahim
Greenhorn
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>();
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
Joined: Jan 28, 2008
Posts: 5575
|
|
Welcome to Javaranch
Your question doesnt have enough information
|
 |
Alexander Cowie
Greenhorn
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 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
Joined: Mar 02, 2005
Posts: 436
|
|
Hi,
Are you trying to insert each character in the String as a new element to the List? Try something like this:-
Cheers,
Raj.
|
 |
 |
|
|
subject: converting a String to an ArrayList
|
|
|