views:

92

answers:

5

In JavaScript I can build an Array of string values like:

var stuff = new Array('foo','bar','baz','boz','gaz','goz');

or even easier

var stuff = 'foo,bar,baz,boz,gaz,goz'.split(',');

In Java it seems overly verbose and complex... is there an easier way than this?

ArrayList<String> stuff = new ArrayList<String>();
stuff.add("foo");
stuff.add("bar");
stuff.add("baz");
stuff.add("boz");
stuff.add("gaz");
stuff.add("goz");

In my actual scenario I have 30-40 items going into the ArrayList... I figure there just has to be an easier way! What is the obvious bit I'm overlooking?

+8  A: 

List<String> strings = Arrays.asList(new String[] {"foo", "bar", "baz"}); or List<String> strings = Arrays.asList("foo,bar,baz".split(","));

splix
`Arrays.asList` accepts varargs parameters. So it can be called as `List<String> strings = Arrays.asList("foo", "bar", "baz")`. Better yet, do an `import static` for `Arrays.asList` and then it becomes even simpler: `List<String> strings = asList("foo", "bar", "baz")`.
abhin4v
@abhin4v: That last one is what I use; it very strongly reduces the amount of noise, and it's in the base libraries too.
Donal Fellows
For an actual `java.util.ArrayList`, you will need to wrap it in `new `java.util.ArrayList<String>( ... )`.
Tom Hawtin - tackline
What Tom said; asList returns an Arrays.ArrayList which is an immutable wrapper around an array.
Andrei Fierbinteanu
Actually it's not exactly immutable, you can't add or remove from it, but you can change the elements (just like in a normal array).
Andrei Fierbinteanu
+1  A: 

Arrays.asList() is a good way to get a List implementation, though it is technically not an ArrayList implementation, but an internal type.

If you truly need it to be an ArrayList you could write a quick utility method:

public static List<String> create(String... items) {
    List<String> list = new ArrayList<String>(items.length);
    for (String item : items) {
        list.add(item);
    }

    return list;
}
Jeff
I should add that splix has a fine solution and you probably don't care about the actual implementation, but you will have to change you variable declaration from 'ArrayList<String> stuff' to 'List<String> stuff'
Jeff
+1  A: 

If you're jamming 30-40 items into a list at compile time, that might be symptomatic of a greater ill. It depends a lot on what you're implementing. You can still use split, if you really want though. Then just do a mass insert to your list.

j flemm
A: 

With the caveat that I'm just learning Java's Collections myself, the following compiles and runs for me:

import java.util.*;
class ArrayListStrings {
    public static void main(String[] args) {
        String[] stuffArray = {"foo","bar","baz","boz","gaz","goz"};
        ArrayList<String> stuff = new ArrayList<String>();
        stuff.addAll(Arrays.asList(stuffArray));
        for (int i = 0; i < stuff.toArray().length; i++)
            System.out.println(stuff.toArray()[i]);   
    }
}

The output is:

foo
bar
baz
boz
gaz
goz

GreenMatt
A: 

how abt the below code using http://code.google.com/p/guava-libraries/

import static com.google.common.collect.Lists.*;

List<String> stuff = newArrayList("foo","bar","baz","boz","gaz","goz");
Pangea