Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I quite often need to implement collection-like classes and - to make using them more comfortable - would like to take advantage of a constructor with variable arguments.

The following implementation does not work, though.

class FruitsStore
    private List<String> availableFruits;

    public FruitsStore(String... fruits)
        this.availableFruits = Arrays.asList(fruits);

    public void add(String fruit)


The following code snippet

FruitsStore store = new FruitsStore("banana", "melon");

throws the following exception

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at cz.dusanrychnovsky.FruitsStore.add(
    at cz.dusanrychnovsky.FruitsStore.main(

To overcome this issue, I use a constructor like this:

public FruitsStore(String... fruits)
    this.availableFruits = new LinkedList<String>(Arrays.asList(fruits));

This compiles and works fine.

Is this a good coding practice? Does it have any drawbacks? What implementation would you suggest?

share|improve this question… returns a fixed-size list. No add no remove. – abuzittin gillifirca Dec 31 '12 at 14:35
But what is the correct way to get around this issue? – Dušan Rychnovský Dec 31 '12 at 14:53
I don't see why you would need to define a new class just to represent a (crippled) collection of objects and do nothing else with it when you could just use a plain list assigned to an appropriately named variable. What you've shown here doesn't justify the need. – Jeff Mercado Jan 1 '13 at 11:03
The code was only meant to illustrate the problem. The classes always contain other (domain specific) methods to operate on the list, which are not provided by any standard collection classes. I edited the post to make this more obvious (three dots at the end of the class definition). – Dušan Rychnovský Jan 1 '13 at 12:23
up vote 2 down vote accepted

You have found the natural solution to this situation. Arrays.asList will give you a List with specific features - new xxList(Arrays.asList(...)) will give you a list with attributes you require.

share|improve this answer
Does it have any drawbacks, such as low performance? Is there a better solution? – Dušan Rychnovský Jan 1 '13 at 9:24
No and no. What you should really consider is how your collection-like class will be used. For example LinkedList is slower than arraylist on indexed access and is not synchronized. – abuzittin gillifirca Jan 1 '13 at 11:31

Arrays.asList does not create a new collection, rather it provides a List-like interface to the original Array. As an Array can't grow in size, the add() method is unsupported. Even when a fixed size List is acceptable, creating a new list from the original array does use a bit more memory, but may be the safer way to go if you don't need the side affect of changes to the original Array.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.