-1

I have a spec that requires me to pass an array of lists. The array is always length 2. I am using the following to accomplish this:

List<MyClass> [] data = new ArrayList[2];
data[0] = new ArrayList<MyClass>();
data[1] = new ArrayList<MyClass>();

compiles but gives warning:

uses unchecked or unsafe operations.

I understand that Arrays of generics are not allowed in Java however I cannot change the spec and the above code seems to work nicely. As long as I am conscious that I never reassign the elements of the array to be something other than type ArrayList<MyClass> are there any reasons I should not just suppress this warning and be on my way?

thedarklord47
  • 2,494
  • 2
  • 17
  • 45
  • 1
    Best to use a List of List: `List>` – Hovercraft Full Of Eels Jan 09 '17 at 23:34
  • 2
    [You can't create generic arrays.](https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays) – shmosel Jan 09 '17 at 23:35
  • 1
    Possible duplicate of [this](http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java) – Hovercraft Full Of Eels Jan 09 '17 at 23:35
  • Possible duplicate of [What's the reason I can't create generic array types in Java?](http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java) –  Jan 09 '17 at 23:36
  • I understand that this is not the correct way to do this but like I said, I cannot change the spec. Hence, my question about whether it is acceptable to simply suppress the warning. – thedarklord47 Jan 09 '17 at 23:39

3 Answers3

3

The compiler in your case, warning you that your code isn't going to do any checking for you that which type of values you are adding to your array. You can ignore this warning, as long as you are ensuring that only ArrayList<MyClass> type are added to your array.

@SuppressWarnings("unchecked") is present for a reason in java, you can suppress the warning and let your compiler know that you don't need it's type checking.

Amit Bhati
  • 5,215
  • 1
  • 19
  • 42
0

It's OK to suppress the warning given that Java doesn't allow generic array creation. Although it is no safe, there is no other way to create arrays with generics unless you ignore or suppress that warning.

alayor
  • 3,336
  • 4
  • 20
  • 39
  • 2
    Java doesn't allow it for a reason. Why do you assume it's safe to ignore? – shmosel Jan 09 '17 at 23:36
  • I am explicitly setting elements 0 and 1 to objects of type `ArrayList`. Java allows it to compile for a reason too. – thedarklord47 Jan 09 '17 at 23:43
  • @thedarklord47 Mostly because of backward compatibility with pre-generic arrays. Either way, I didn't say it's never acceptable, just that it's not necessarily safe. – shmosel Jan 10 '17 at 00:29
0

You cannot create Generic Arrays; see the official Java documentation on the subject.

You can still get rid of the compile-time warning, like so...

List[] data = new List[2];

Of course, this means that you need to check the type of everything going in to/coming out of the Lists when you start referencing their data & casting it appropriately. So be wary.

nasukkin
  • 2,282
  • 8
  • 19