Collection-like classes in Java - taking advantage of varargs 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&lt;String&gt; availableFruits;

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

    public void add(String fruit)
    {
        this.availableFruits.add(fruit);
    }

    ...
}


The following code snippet

FruitsStore store = new FruitsStore("banana", "melon");
store.add("orange");


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(FruitsStore.java:26)
    at cz.dusanrychnovsky.FruitsStore.main(FruitsStore.java:12)


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

public FruitsStore(String... fruits)
{
    this.availableFruits = new LinkedList&lt;String&gt;(Arrays.asList(fruits));
}


This compiles and works fine.

Is this a good coding practice? Does it have any drawbacks? What implementation would you suggest?
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.