0

我正在和我的同学创建一个全班项目,我应该为某些功能创建拉取请求,但是我在创建类并以一种可以编译的方式覆盖方法时遇到了一些问题(我现在不需要写方法,那是项目,我只需要它来编译)。我发现大多数方法都有效(或者至少编译器没有抱怨),但我对一些事情感到困惑:

  • 编译器抱怨可选的方法(如 set 和 addAll)

  • 方法 addAll,虽然它被添加,但抱怨它没有被覆盖,虽然它是,当我为它添加另一个 addAll 方法时,我也得到一个擦除错误。

我已经阅读了很多关于它的内容,但我找不到关于如何解决它的正确结论。我只是使用 Atom 来编写我的代码和终端,没有花哨的 IDE(也许我应该学习一个)。

如果不清楚,我只是希望有可用的方法的存根,而不是每个方法的全面答案,因为这是有类的项目。

  // https://docs.oracle.com/javase/8/docs/api/java/util/List.html

import java.util.*;
import java.lang.reflect.*;

public class SkipList<E> implements List<E>
{
    // compiler complaining, then added, although optional
    public E set(int index, E element)
    {
        throw new IndexOutOfBoundsException();
    }

    // compiler complaining, then added, although optional
    public boolean addAll(Collection <? extends E> c)
    {
        return true;
    }

    // Group 1
    public boolean add(E e)
    {
        return true;
    }

    public void add(int index, E e)
    {

    }

    public boolean addAll(Collection c)
    {
        return true;
    }

    public int indexOf(Object o)
    {
        int index = 0;
        return index;
    }

    public int lastIndexOf(Object o)
    {
        int index = 0;
        return index;
    }

    // Group 2
    public boolean contains(Object o)
    {
        return true;
    }

    public boolean containsAll(Collection c)
    {
        return true;
    }

    public boolean equals(Object o)
    {
        return true;
    }


    public List<E> subList(int fromIndex, int toIndex)
    {
        List<E> sub = new SkipList<>();
        return sub;
    }

    // Group 3
    public boolean isEmpty()
    {
        return true;
    }

    public int size()
    {
        int size = 0;
        return size;
    }

    public void clear()
    {

    }

    public E get(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    public E getQuantile(double quantile) // e.g. 0 = minimum, 0.5 = median, 1 = max
    {
        throw new IndexOutOfBoundsException();
    }

    // Group 4
    public Iterator<E> iterator()
    {
        throw new IndexOutOfBoundsException();
    }

    public ListIterator<E> listIterator()
    {
        throw new IndexOutOfBoundsException();
    }

    public ListIterator<E> listIterator(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    // Group 5
    public E remove(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    public boolean remove(Object o)
    {
        return true;
    }

    public boolean removeAll(Collection c)
    {
        return true;
    }

    public boolean retainAll(Collection c)
    {
        return true;
    }

    // Group 6
    public int hashCode()
    {
        int hashCode = 0;
        return hashCode;
    }

    public Object[] toArray()
    {
        Object[] arr = new Object[0];
        return arr;
    }

    public <T> T[] toArray(T[] a)
    {
        return a;
    }
}
4

1 回答 1

0

事实证明,在阅读 API 时,出于某种原因,我忽略了 addAll 方法的另一个参数,这让我相信还有其他问题。我用正确的参数更改了方法并编译了它。

public boolean addAll(int index, Collection <? extends E> c)
{
    return true;
}
于 2017-10-21T04:41:57.323 回答