Re: array of lists



Thomas Hawtin wrote:
Wesley Hall wrote:

Seems there is lots of discussion on the creation of generic arrays in
Java, but the bottom line seems to be that you cant do it in any
reasonable way.

Just don't use them. With generics there is little use for arrays of
reference types. Just use a List.


Fair point.

If you just want to clear the compiler warning, put the following
annotation on the method that does the add..

@SuppressWarnings({"unchecked"})

That is a measure of last resort. Not something to be thrown around just
to make the compiler shut up. If the compiler is making a noise, there
is probably a good reason.

In this case, it seems to be that the compiler is moaning that the OP is
using an array with no generic type, but as there seems to be no way to
create an array WITH a generic type (no reasonable way at least) then it
is not possible to avoid the compiler warning and still use arrays.
Using a list (as per you first point) is a reasonable measure but is not
always an option. There is still plenty of third party code that
requires the use of reference type arrays.

I don't agree that the above annotation is 'the measure of a last
resort'. A warning is exactly that, a warning. Once the developer has
analysed the problem, determining that the warning is valid but doesn't
pose any problem then using the annotation to suppress the warning is a
good move. It will prevent the next developer to compile the code from
wasting time analysing a superfluous warning.
.



Relevant Pages

  • Re: A question related to type casting
    ... cast" warning for the cast to a raw List. ... compiler passes the code, ... I prefer no warnings when I compile, but they aren't a restriction on the code itself, and I'm not seeing the warning you describe anyway. ... The only reason it works now is that generics are strictly compile-time, and so there's not actually any difference between the raw List type and a parameterized reference to the generic List. ...
    (comp.lang.java.programmer)
  • Re: Basic question #4
    ... you can only catch them in simulation. ... Until runtime the compiler has no idea ... what the generics are going to be set to. ... So no warning can come up. ...
    (comp.lang.vhdl)
  • Re: How can I cast to an array type?
    ... out how to cast the char* to the array type to remove the warning. ... the compiler is warning you about a conversion ... between incompatible pointer types. ... Arrays are usually manipulated via a pointer to the ...
    (comp.lang.c)
  • Re: [unchecked] unchecked call to add(E) --using ArrayList.
    ... > I'm getting the following warning when adding an instance of my class 'film' ... You're using Java 1.5 which implements generics. ... What the compiler is telling ... type Film into the ArrayList. ...
    (comp.lang.java.help)
  • Re: A question related to type casting
    ... cast" warning for the cast to a raw List. ... the compiler passes the code, ... I prefer no warnings when I compile, but they aren't a restriction on the code itself, and I'm not seeing the warning you describe anyway. ... The only reason it works now is that generics are strictly compile-time, and so there's not actually any difference between the raw List type and a parameterized reference to the generic List. ...
    (comp.lang.java.programmer)