• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add comma seperated string obejects into a ArrayList  RSS feed

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,
How to add comma seperated string objects into a ArrayList?

Thank you,
Me
 
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Snipped, based on below.
Thanks to all and a thousand apologies to some.
[ June 21, 2008: Message edited by: Taariq San ]
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, and
  • a 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
    Posts: 192
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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, and
  • a 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
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome I have been caught like that myself.
     
    Author
    Ranch Hand
    Posts: 836
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!