1

To start with there are multiple posts on SO itself which explain why Generics and arrays do not mix in Java.

Like Generic arrays in Java

But my question is - if that is so why do I get a warning in Eclipse when I do something like the following and how to get rid of this warning message.

private RotatingQueue<RotatingQueueData> rQueue[] =  new RotatingQueue[15];

Warning: Multiple markers at this line - Type safety: The expression of type RotatingQueue[] needs unchecked conversion to conform to RotatingQueue[]

7
  • 2
    What happens if you change the right hand side of the assignment to new RotatingQueue<RotatingQueueData>[15] ? Commented Mar 31, 2014 at 8:17
  • Even better: new RotatingQueue<>[15]? (Java 7 is out, even Java 8) Commented Mar 31, 2014 at 8:18
  • Yes, I know, but I didn't want to introduce a new unknown, given that we don't know what version Andy is running. Commented Mar 31, 2014 at 8:22
  • 4
    @DavidWallace & LutzHorn Neither of those would work. Creation of array of parameterized type is not allowed. Commented Mar 31, 2014 at 8:22
  • 1
    @Andy897 Yup. You can suppress it though. Even better would be avoid create array of parameterized types. Rather use List<RotatingQueue<RotatingQueueData>>. Commented Mar 31, 2014 at 8:30

1 Answer 1

3

It is not possible to create an array of a parameterized type. Hence you have to live with the warning, but may ignore it like this:

@SuppressWarnings("unchecked")
List<String> lists[] = new List[15];

I don't know about any other way to do this if you try to continue to work with List<String>.

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

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.