문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top