16

Setting a list of values for a Java ArrayList works:

Integer[] a = {1,2,3,4,5,6,7,8,9};
ArrayList<Integer> possibleValues2 = new ArrayList<Integer>(Arrays.asList(a));

However, the following doesn't work and has the error "Illegal start of type" as well as other. Why not? Since the first line in the first code block is simply assignment, shouldn't it not have an effect?

ArrayList<Integer> possibleValues2 = new ArrayList<Integer>(Arrays.asList({1,2,3,4,5,6,7,8,9}));
4
  • 6
    Wow. A lot of points being given out considering that no one is actually answering the question. The OP didn't ask what the other options were, but why this doesn't work. Duplicate of stackoverflow.com/questions/3504849/java-weird-array-behavior. Commented Jan 21, 2011 at 21:52
  • 1
    @Robin But the answers are nevertheless mostly true and insightful. The trivial standard answer to the literal question is often not the best answer. Commented Jan 21, 2011 at 22:03
  • Since my answer was being unjustly downvoted I deleted it. However, here's a previous answer to a similar question: stackoverflow.com/questions/4324633/… Commented Jan 21, 2011 at 22:19
  • @Robin: Ouch, I just realized I was reading too much into the question. It looked like it was about generics and int/integers, that my eyes just glossed over the simpler issue of the curly braces in there. Commented Jan 21, 2011 at 22:26

4 Answers 4

28

You should use either the vararg version of Arrays.asList, e.g.

ArrayList<Integer> possibleValues2 =
    new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));

or explicitly create an array parameter, e.g.

ArrayList<Integer> possibleValues2 =
    new ArrayList<Integer>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9}));
Sign up to request clarification or add additional context in comments.

Comments

9

A strange and little used idiom,

List<Integer> ints = new ArrayList<Integer>() {{add(1); add(2); add(3);}}

This is creating an anonymous class that extends ArrayList (outer brackets), and then implements the instance initializer (inner brackets) and calls List.add() there.

The advantage of this over Arrays.asList() is that it works for any collection type:

Map<String,String> m = new HashMap<>() {{ 
  put("foo", "bar");
  put("baz", "buz"); 
  ...
}}

6 Comments

It's of course nice to know this and definitely valueable for interviews as this is a common question, but I'd consider it a bad practice to create anonymous subclasses of standard classes like ArrayList. From an OOP viewpoint, a class should only be extended if the new class has functionality the old class misses. And it will litter your code base with subclasses names OuterClass$1.class or similar.
that's true, but i'd only take it to heart if you plan on re-using the class. anonymous classes are for inline convenience not reuse. or, another way to look at it ... i'm creating a new subtype of ArrayList that initializes itself to a set of static values. the functionality is initialization of the given set of static values.
I'd still always prefer new ArrayList<Integer>(Arrays.asList(1,2,3)). It's nice ans sinple, and it leaves the class hierchy untouched
Arrays.asList() is fine for lists, but the above idiom works for any collection type, or any object for that matter.
Does it affect performance in some way? I already figured it affects serialization.
|
4

Another option is to use Guava ("Google collections"), which has a Lists.newArrayList(...) method.

Your code would be something like

ArrayList<Integer> possibleValues2 = Lists.newArrayList(1,2,3,4,...);

7 Comments

Although this answer is very nice (+1), it would fail with the same error as Arrays.asList() when called with an int[] array
For an int[] array, Lists.newArrayList(Ints.asList(intArray)) would work though.
Very strange. I just ran the following on my machine: public void testArrayList() { ArrayList<Integer> list = Lists.newArrayList(3,4); assertEquals("[3, 4]", list.toString()); } And had no problems whatsoever, but I might be working with a newer version of Guava.
@Uri, @Sean, @ColinD, I think you need to read the question more carefully. There is no int[] array (it's Integer[].) The problem is the braces. Using a different method to create the list will not help. Also the OP is probably a Java newbie and would not benefit from adding another large library to his project.
@finnw: Agreed, my bad. I looked at the question, misunderstood it to be about int arrays and/or generics, saw Peter's answer, and apparently answered the wrong thing :)
|
0

From Java 7 SE docs:

List<Integer> possibleValues2 = Arrays.asList(1,2,3,4,5,6,7,8,9);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.