0

I'm trying to define an array for 3 ArrayLists, each containing a double array. I've used the following syntax:

ArrayList<double[]> testSamples[] = new ArrayList[] {
    new ArrayList<double[]>(), new ArrayList<double[]>(), new ArrayList<double[]>()
};

However this generates a "warning: [unchecked] unchecked conversion" warning when I compile the code. Note: the code works fine, I'm just tried to fix my syntax (correctly) to resolve the warning. Since the code works, it appears to be supported by Java, I'm baffled as to why I can't write something like (which generates a compile error):

ArrayList<double[]> testSamples[] = new ArrayList<double[]>[] {
        new ArrayList<double[]>(), new ArrayList<double[]>(), new ArrayList<double[]>()
};

What am I doing wrong?

7
  • Yes it compiles and runs ok, however the compiler outputs the given warning. Commented Jan 10, 2014 at 18:43
  • You can't create an array of parameterized types. Check this post stackoverflow.com/q/18581002/1679863 for further explanation. Commented Jan 10, 2014 at 18:43
  • An ArrayList, that takes type double, being initialized by several ArrayList types? Commented Jan 10, 2014 at 18:44
  • @RohitJain Although he isn't doing that in his snippet. Commented Jan 10, 2014 at 18:45
  • Your array creation expression is not parameterized , it is raw, which is why you get the warning. Commented Jan 10, 2014 at 18:46

2 Answers 2

4

You're implicitly converting from an array of the raw type ArrayList to an array of ArrayList<double[]>s. All in all, you shouldn't do something like this. If you know you will have 3 lists, then you can create a class to hold them instead:

class Container {
    private ArrayList<double[]> list1;
    private ArrayList<double[]> list2;
    private ArrayList<double[]> list3;

    ...  // constructors and whatnot

    public ArrayList<double[]> getList(int i) {  // analog of testSamples[i]
        switch (i) {
        case 0:
            return list1;
        case 1:
            return list2;
        case 2:
            return list3;
        default:
            throw new IllegalArgumentException();
        }
    }
}

If the number of lists isn't fixed at 3, then you can use a List<ArrayList<double[]>> instead.

Sign up to request clarification or add additional context in comments.

11 Comments

Much better alternative.
This is definitely easier on the eyes, however the collections are being read and written within loops, and I wanted to avoid using if/switch statements to determine which collection variable to read/write. The code is much more concise if I can just write testSamples[i].
@JJ. See the edit -- now you can simply do container.get(i).
Sure, refactoring the switch logic into a separate method helps but is still a workaround for a warning that seems to not matter (the code works fine). If no one else solves the original problem you'll get the credit.
@JJ. The code may work as-is but it's terrible practice. In any case, if all you care about is the warning itself, then you can simply use @SuppressWarnings. But again, this just hides the underlying problem.
|
1

This is the usual problem with creating an array of a parameterized type. Why you cannot use new to create an array of a parameterized type is a long topic that has been covered many times here; it basically has to do with how arrays perform runtime checks on the element types, but runtime checks cannot check type parameters.

Long story short, an unchecked warning of some kind is unavoidable (because it is indeed possible to make it violate the guarantees of the array type). If you don't mind the "unsafeness", the most kosher way to write it is to create the array using a wildcarded type instead of a raw type, and cast it to the proper type of array (yes, this is an unchecked cast):

ArrayList<double[]>[] testSamples =
    (ArrayList<double[]>[]) new ArrayList<?>[] { ... }

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.