String to ArrayList

A

Angry Moth Town

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.
 
A

Andrea Francia

Angry 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.


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[])
 
E

Eric Sosman

Angry 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));
}


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."
 
S

Steven Simpson

Angry said:
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*")));
}
 
S

Steven Simpson

[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*")));
^^^^^^^
 
M

Mark Space

Andrea said:
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.
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
A

Andreas Leitgeb

Roedy Green said:
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.
 
J

Jeffrey H. Coffield

Angry 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.


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
 
A

Angry Moth Town

Angry 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));
}


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?
 
L

Lord Zoltar

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?
 
A

Angry Moth Town

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.
 
M

Mark Space

Angry said:
} 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top