| Author |
How to add comma seperated string obejects into a ArrayList
|
Ramna Reddy
Ranch Hand
Joined: Aug 06, 2006
Posts: 96
|
|
HI All, How to add comma seperated string objects into a ArrayList? Thank you, Me
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
String#split(",") - helps to store comma separated string objects into array and then using for each loop , add those value into List object !! Hope this help !
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Taariq San
Ranch Hand
Joined: Nov 20, 2007
Posts: 189
|
|
Originally posted by Sagar Rohankar: String#split(",") - helps to store comma separated string objects into array and then using for each loop , add those value into List object !! Hope this help !
String's split is a good idea, though instead of looping you could use Arrays' asList method, takes an array and returns an ArrayList. Or, you could just construct an ArrayList with that array from String's split.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Taariq San: String's split is a good idea, though instead of looping you could use Arrays' asList method, takes an array and returns an ArrayList.
Actually it returns a List (which may or may not be an Arraylist). If you want to use any of the ArrayList specific methods, you shouldn't do it this way.
Originally posted by Taariq San: you could just construct an ArrayList with that array from String's split.
ArrayList doesn't have a constructor that accepts an array as an argument. [ June 20, 2008: Message edited by: Joanne Neal ]
|
Joanne
|
 |
Taariq San
Ranch Hand
Joined: Nov 20, 2007
Posts: 189
|
|
Snipped, based on below. Thanks to all and a thousand apologies to some. [ June 21, 2008: Message edited by: Taariq San ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Originally posted by Taariq San: Stick with List you get back from asList, it's an ArrayList.
Beware. There are two classes called ArrayList. There is java.util.ArrayList<E>, which is a public class implementing the List<E> interface, anda private member class called ArrayList inside the Arrays class.What you get from Arrays.asList() is the latter; it is fixed-size, but allows one to change the original array. Don't get confused between the two. I think it is unfortunate that they chose the name ArrayList for two different classes, because of the potential confusion. Only call the latter List<E>; you cannot give it a class name.
|
 |
Taariq San
Ranch Hand
Joined: Nov 20, 2007
Posts: 189
|
|
Originally posted by Campbell Ritchie: Beware. There are two classes called ArrayList. There is java.util.ArrayList<E>, which is a public class implementing the List<E> interface, anda private member class called ArrayList inside the Arrays class.What you get from Arrays.asList() is the latter; it is fixed-size, but allows one to change the original array. Don't get confused between the two. I think it is unfortunate that they chose the name ArrayList for two different classes, because of the potential confusion. Only call the latter List<E>; you cannot give it a class name.
Ouch, that's Arrays' inner class. Thanks Campbell.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
You're welcome I have been caught like that myself.
|
 |
Charles Lyons
Author
Ranch Hand
Joined: Mar 27, 2003
Posts: 836
|
|
In answer to the original question, I guess the most efficient way (in terms of memory and CPU) is just to do it:I think that works, but it's just out of my head, so you'd need to check it! Just as efficient would be to use the split method on String and create a String[] - if you can't handle an array, perhaps writing your own List wrapper class around the array would do (rather than copying all the entries in which is going to double-up the overall number of iterations)? I think there's very little reason to require the ArrayList implementation instead of a generic List type - is there really any difference in the API?
|
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
|
 |
 |
|
|
subject: How to add comma seperated string obejects into a ArrayList
|
|
|