Pergunta

I have the following code:

public class DEF implements Set<ABC> {

   private EnumSet<ABC> xyz=EnumSet.noneOf(ABC.class);

   @Override
   public <T> T[] toArray(T[] a) {
       return xyz.toArray(a);
   }

}

Which gives me the following warning:

Array of type 'ABC[]' expected at line 43

Is this dangerous? Or can I ignore it? Why is this warning?

Foi útil?

Solução

The warning is completely correct; your code makes no sense.

By writing <T> T[] toArray(), you've made a function that can be called with any type parameter and will return an array of that type.
Always returning an array of ABCs violates what you claimed it will do.

In short, that function should not be generic.


None of this is applicable to your case, because the base Set<E> interface requires this.

I have no idea why Set<E> is declared that way, but there is nothing you can do about the warning.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top