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[i] = pieces[i].trim();
}
return new ArrayList(Arrays.asList(pieces));
}
Thank you in advance.
Andrea Francia - 13 Mar 2008 18:25 GMT
> 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:
[quoted text clipped - 9 lines]
>
> 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[])

Signature
Andrea Francia
http://www.andreafrancia.it/
Mark Space - 13 Mar 2008 21:10 GMT
> 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.
Eric Sosman - 13 Mar 2008 18:28 GMT
> 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:
[quoted text clipped - 7 lines]
> 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[i].trim());
}
return result;
}
Which of these appeals to you (if any) depends on your
own definitions of "better" and "cleaner."

Signature
Eric.Sosman@sun.com
Angry Moth Town - 18 Mar 2008 14:51 GMT
> > 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:
[quoted text clipped - 29 lines]
> --
> Eric.Sos...@sun.com
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?
Lord Zoltar - 18 Mar 2008 15:07 GMT
> 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?
Angry Moth Town - 18 Mar 2008 16:31 GMT
> > 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
[quoted text clipped - 4 lines]
> 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.
Mark Space - 18 Mar 2008 18:16 GMT
> } 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.
Steven Simpson - 13 Mar 2008 20:11 GMT
> public static ArrayList stringToArrayList(String list, String separator) {
> String[] pieces = list.split(separator);
[quoted text clipped - 4 lines]
> }
>
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*")));
}

Signature
ss at comp dot lancs dot ac dot uk |
Steven Simpson - 13 Mar 2008 20:30 GMT
>> 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*")));
^^^^^^^

Signature
ss at comp dot lancs dot ac dot uk |
Roedy Green - 14 Mar 2008 07:07 GMT
On Thu, 13 Mar 2008 10:07:12 -0700 (PDT), Angry Moth Town
<bigdave.smith@gmail.com> 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:
[quoted text clipped - 9 lines]
>
>Thank you in advance.
think about how you might define your separator to trim as a side
effect of splitting.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 14 Mar 2008 07:08 GMT
On Thu, 13 Mar 2008 10:07:12 -0700 (PDT), Angry Moth Town
<bigdave.smith@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>for (int i = pieces.length - 1; i >= 0; i--) {
> pieces[i] = pieces[i].trim();
> }
You could use a for:each loop. There is no particular reason to do it
backwards.. This may defeat lookahead caching.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andreas Leitgeb - 14 Mar 2008 11:36 GMT
><bigdave.smith@gmail.com> wrote:
>>for (int i = pieces.length - 1; i >= 0; i--) {
>> pieces[i] = pieces[i].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.
Jeffrey H. Coffield - 16 Mar 2008 00:19 GMT
> 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:
[quoted text clipped - 9 lines]
>
> 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