The warning is just, well, warning you that the objects in the cars
array that are being casted aren't guaranteed to be an ObjectArrayList<Car>
.
It turns out Java doesn't allow array declaration with generic types unless they're unbounded (see Java 1.6: Creating an array of List, or this bug report 6229728 : Allow special cases of generic array creation). But if you could declare the array like this, you wouldn't be getting the warning:
final ObjectArrayList<Car>[] cars=new ObjectArrayList<Car>[1000]; // not allowed
If you really want to avoid an unchecked cast you should use a strongly typed collection instead of an array (for instance, a List<ObjectArrayList<Car>>
).
The @SuppressWarnings("unchecked")
annotation will just tell the compiler not to show the warning. I wouldn't advice using it unless the warning really annoys you. Reality is you'll be doing an unchecked cast (not a problem if you're certain about the type of the elements in the array).
As a side note, annotations aren't in any way deprecated in Java. Actually, it seems they'll become more powerful with Java 8 (it seems they'll support JSR 308: Annotations on Java Types). Maybe you read that @Deprecated
is an annotation instead.