The class must either be declared abstract, or implement the abstract toArray method

I have a class Foothat extends AbstractListand implements List. This class implements some of the methods List, but some just throw UnsupportedOperationException.

toArrayis one of the last, and although the compiler does not complain about other methods that are not really implemented, it complains about the error toArray:

Class must either be declared abstract or implement abstract method toArray(T[]) in List.

public class Foo extends AbstractList implementst List  {
    ...
     public <T> T[] toArray(T[] a) throws UnsupportedOperationException {
        throw new UnsupportedOperationException(error);
    }
}

What is wrong here and why does the compiler still think that the method is toArray(T[])not implemented?

+4
source share
2 answers

public T [] toArray (T [] a), , . :

public class Foo<T> extends AbstractList<T> implements List<T> {

    @Override
    public <E> E[] toArray(E[] a) throws UnsupportedOperationException {
        throw new UnsupportedOperationException("Error!");
    }

    ...
}
+2

:

import java.util.AbstractList;
import java.util.List;

public class Foo extends AbstractList implements List  {

    @Override
    public Object[] toArray(Object[] a) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }
}

java.util.

-1

Source: https://habr.com/ru/post/1673785/


All Articles