String to ArrayList

Discussion in 'Java' started by Angry Moth Town, Mar 13, 2008.

  1. Hi all. I'm fairly new to java. Is there a better or cleaner way to
    convert a String to an ArrayList in Java 1.4 than this:

    public static ArrayList stringToArrayList(String list, String
    separator) {
    String[] pieces = list.split(separator);
    for (int i = pieces.length - 1; i >= 0; i--) {
    pieces = pieces.trim();
    }
    return new ArrayList(Arrays.asList(pieces));
    }

    Thank you in advance.
     
    Angry Moth Town, Mar 13, 2008
    #1
    1. Advertising

  2. Angry Moth Town wrote:
    > Hi all. I'm fairly new to java. Is there a better or cleaner way to
    > convert a String to an ArrayList in Java 1.4 than this:
    >
    > public static ArrayList stringToArrayList(String list, String
    > separator) {
    > String[] pieces = list.split(separator);
    > for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }
    > return new ArrayList(Arrays.asList(pieces));
    > }
    >
    > Thank you in advance.


    import java.util.Arrays;
    ...
    Arrays.asList(list.split(separator));


    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#asList(java.lang.Object[])

    --
    Andrea Francia
    http://www.andreafrancia.it/
     
    Andrea Francia, Mar 13, 2008
    #2
    1. Advertising

  3. Angry Moth Town

    Eric Sosman Guest

    Angry Moth Town wrote:
    > Hi all. I'm fairly new to java. Is there a better or cleaner way to
    > convert a String to an ArrayList in Java 1.4 than this:
    >
    > public static ArrayList stringToArrayList(String list, String
    > separator) {
    > String[] pieces = list.split(separator);
    > for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }
    > return new ArrayList(Arrays.asList(pieces));
    > }


    If you must have an ArrayList, this seems good enough and
    clean enough. Changing the "contract" to return a List might
    be a bit cleaner, from some points of view, and would eliminate
    the constructor call. Still another possibility would be to
    retain ArrayList but to build it incrementally instead of all
    at one go:

    public static ArrayList ... {
    String[] pieces = ...;
    ArrayList result = new ArrayList(pieces.length);
    for (int i = 0, n = pieces.length; i < n; ++i) {
    result.add(pieces.trim());
    }
    return result;
    }

    Which of these appeals to you (if any) depends on your
    own definitions of "better" and "cleaner."

    --
     
    Eric Sosman, Mar 13, 2008
    #3
  4. Angry Moth Town wrote:
    > public static ArrayList stringToArrayList(String list, String separator) {
    > String[] pieces = list.split(separator);
    > for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }
    > return new ArrayList(Arrays.asList(pieces));
    > }
    >


    Could you surround the separator with patterns to match characters that
    String.trim() takes off?

    public static ArrayList stringToArrayList(String list, String separator) {
    return new ArrayList(Arrays.asList(list.split("\s*" + separator + "\s*")));
    }

    --
    ss at comp dot lancs dot ac dot uk |
     
    Steven Simpson, Mar 13, 2008
    #4
  5. Steven Simpson wrote:
    > Angry Moth Town wrote:
    >> public static ArrayList stringToArrayList(String list, String
    >> separator) {

    [splitting, trimming, then making an ArrayList]
    > Could you surround the separator with patterns to match characters
    > that String.trim() takes off?

    Ah, that won't trim leading space from the first element, nor trailing
    from the last; trim before splitting:

    return new ArrayList(Arrays.asList(list.trim().split("\s*" + separator + "\s*")));
    ^^^^^^^

    --
    ss at comp dot lancs dot ac dot uk |
     
    Steven Simpson, Mar 13, 2008
    #5
  6. Angry Moth Town

    Mark Space Guest

    Andrea Francia wrote:

    > import java.util.Arrays;
    > ...
    > Arrays.asList(list.split(separator));


    This is pretty good, but we just had someone yesterday or the day before
    post here, asking why .split() didn't work. His seperator was "." which
    is a special character in regular expressions.

    Granted the OP was using his separator the same way you did, so this
    won't introduce any more problems. But I thought I'd point out to the
    OP and folks in general that this method may give unexpected results if
    you aren't up on your regular expressions.
     
    Mark Space, Mar 13, 2008
    #6
  7. Angry Moth Town

    Roedy Green Guest

    On Thu, 13 Mar 2008 10:07:12 -0700 (PDT), Angry Moth Town
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Hi all. I'm fairly new to java. Is there a better or cleaner way to
    >convert a String to an ArrayList in Java 1.4 than this:
    >
    > public static ArrayList stringToArrayList(String list, String
    >separator) {
    > String[] pieces = list.split(separator);
    > for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }
    > return new ArrayList(Arrays.asList(pieces));
    > }
    >
    >Thank you in advance.


    think about how you might define your separator to trim as a side
    effect of splitting.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 14, 2008
    #7
  8. Angry Moth Town

    Roedy Green Guest

    On Thu, 13 Mar 2008 10:07:12 -0700 (PDT), Angry Moth Town
    <> wrote, quoted or indirectly quoted someone
    who said :

    >for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }


    You could use a for:each loop. There is no particular reason to do it
    backwards.. This may defeat lookahead caching.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 14, 2008
    #8
  9. Roedy Green <> wrote:
    ><> wrote:
    >>for (int i = pieces.length - 1; i >= 0; i--) {
    >> pieces = pieces.trim();
    >> }

    > You could use a for:each loop.


    The for:each loop wouldn't help him much writing the
    trimmed strings back to the array.

    But he could still use a for:each loop to iterate the
    array and add the trimmed strings to the arraylist, instead
    of writing them back and then copying them forward again.

    Even better is still, what was written in another f'up, namely
    to change the splitting such, that the trimming happens implicitly,
    so the for-loop can be dropped completely. (note, that in this
    case you'd need to trim the complete initial string, first, or you
    might see whitespace in front of the first or trailing on the last
    part after splitting.
     
    Andreas Leitgeb, Mar 14, 2008
    #9
  10. Angry Moth Town wrote:
    > Hi all. I'm fairly new to java. Is there a better or cleaner way to
    > convert a String to an ArrayList in Java 1.4 than this:
    >
    > public static ArrayList stringToArrayList(String list, String
    > separator) {
    > String[] pieces = list.split(separator);
    > for (int i = pieces.length - 1; i >= 0; i--) {
    > pieces = pieces.trim();
    > }
    > return new ArrayList(Arrays.asList(pieces));
    > }
    >
    > Thank you in advance.


    Try this :

    ArrayList a3 = stringToArrayList("a,b,c,", ",");
    System.out.println("a3 length = " + a3.size());

    Do you expect to have 3 or 4 elements?

    Jeff Coffield
     
    Jeffrey H. Coffield, Mar 15, 2008
    #10
  11. On Mar 13, 1:28 pm, Eric Sosman <> wrote:
    > Angry Moth Town wrote:
    > > Hi all. I'm fairly new to java. Is there a better or cleaner way to
    > > convert a String to an ArrayList in Java 1.4 than this:

    >
    > > public static ArrayList stringToArrayList(String list, String
    > > separator) {
    > > String[] pieces = list.split(separator);
    > > for (int i = pieces.length - 1; i >= 0; i--) {
    > > pieces = pieces.trim();
    > > }
    > > return new ArrayList(Arrays.asList(pieces));
    > > }

    >
    > If you must have an ArrayList, this seems good enough and
    > clean enough. Changing the "contract" to return a List might
    > be a bit cleaner, from some points of view, and would eliminate
    > the constructor call. Still another possibility would be to
    > retain ArrayList but to build it incrementally instead of all
    > at one go:
    >
    > public static ArrayList ... {
    > String[] pieces = ...;
    > ArrayList result = new ArrayList(pieces.length);
    > for (int i = 0, n = pieces.length; i < n; ++i) {
    > result.add(pieces.trim());
    > }
    > return result;
    > }
    >
    > Which of these appeals to you (if any) depends on your
    > own definitions of "better" and "cleaner."
    >
    > --
    >


    Eric (and all),

    Thanks for the help on this. One more item... the original piece of
    code which I am trying to clean up (it was 50 lines) had the method
    throwing an Exception object. In my new method, I've removed this
    throw. Is there, in fact, any reason that this method would need to
    throw an exception?
     
    Angry Moth Town, Mar 18, 2008
    #11
  12. Angry Moth Town

    Lord Zoltar Guest


    >
    > Thanks for the help on this.  One more item... the original piece of
    > code which I am trying to clean up (it was 50 lines) had the method
    > throwing an Exception object.  In my new method, I've removed this
    > throw.  Is there, in fact, any reason that this method would need to
    > throw an exception?


    What sort of exception was it? Under what conditions did it throw the
    exception? Will that condition still exist in your new code?
     
    Lord Zoltar, Mar 18, 2008
    #12
  13. On Mar 18, 10:07 am, Lord Zoltar <> wrote:
    > > Thanks for the help on this. One more item... the original piece of
    > > code which I am trying to clean up (it was 50 lines) had the method
    > > throwing an Exception object. In my new method, I've removed this
    > > throw. Is there, in fact, any reason that this method would need to
    > > throw an exception?

    >
    > What sort of exception was it? Under what conditions did it throw the
    > exception? Will that condition still exist in your new code?


    Lord Zoltar,

    The exception was of type Exception, and the entire method was simply
    surrounded in a try, with a catch caught and threw Exception e:

    public static ArrayList alConvert(String strInputParam,String
    strSeparator) throws Exception
    {
    int intStart=0;

    try {
    //50 lines of code...
    } catch (Exception e) {
    throw e;
    }
    }// end of method

    I removed the try/catch, and the throws Exception, since I didn't see
    any reason to have them, and Eclipse didn't complain, but I'm fairly
    new to Java, so I don't know if I'm supposed to be catching something
    in the event that a parameter is null, or something like that.

    Sorry if that is a bit vague.
     
    Angry Moth Town, Mar 18, 2008
    #13
  14. Angry Moth Town

    Mark Space Guest

    Angry Moth Town wrote:

    > } catch (Exception e) {
    > throw e;
    > }


    This is a terrible idea. If you are new to Java, you get a pass, but
    don't ever do this.

    Exceptions are there to help you debug. First, never catch all
    exceptions like this, because your programs will just fail and never
    tell you why.

    Always catch only the exceptions you must, and when you throw an
    exception, throw the most specific you can. If no existing exception
    matches your needs, make your own and throw that.

    public class MySpecificException extends Exception {}

    Second, there's no point to catching an exception, and then throwing the
    same exception again. That's just kinda silly. It's a lot of work for
    basically doing nothing.

    Last, if you code doesn't need to throw exceptions, then it's fine to
    leave off the catch.
     
    Mark Space, Mar 18, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,750
    Natty Gur
    Aug 19, 2003
  2. Marc Bishop
    Replies:
    1
    Views:
    401
    Codemonkey
    Nov 6, 2003
  3. Kaidi
    Replies:
    4
    Views:
    2,398
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,390
  5. Philipp
    Replies:
    6
    Views:
    933
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page