Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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)
    {
        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<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
    
docs.oracle.com/javase/6/docs/api/java/util/… 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
add comment

2 Answers

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
1  
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
add comment

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
add comment

Your Answer

 
discard

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.