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[]

Community
  • 1
  • 1
Andy897
  • 5,925
  • 8
  • 39
  • 80
  • 2
    What happens if you change the right hand side of the assignment to `new RotatingQueue[15]` ? – Dawood ibn Kareem Mar 31 '14 at 08:17
  • Even better: `new RotatingQueue<>[15]`? (Java 7 is out, even Java 8) –  Mar 31 '14 at 08: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. – Dawood ibn Kareem Mar 31 '14 at 08:22
  • 4
    @DavidWallace & LutzHorn Neither of those would work. Creation of array of parameterized type is not allowed. – Rohit Jain Mar 31 '14 at 08:22
  • @Rohit .. So I have to live with the warning ? – Andy897 Mar 31 '14 at 08:29
  • 1
    @Andy897 Yup. You can suppress it though. Even better would be avoid create array of parameterized types. Rather use `List>`. – Rohit Jain Mar 31 '14 at 08:30
  • Thanks !! I have another pending doubt regarding setting log levels (java.util.logging). I am trying to find a way so that I can specific the log level in my properties file and change it for the whole web app using that. Here is the SO link to it http://stackoverflow.com/questions/22729271/setting-java-log-levels – Andy897 Mar 31 '14 at 08:32

1 Answers1

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>.

Harmlezz
  • 7,524
  • 23
  • 34