繁体   English   中英

编译器要求重写Collections中的可选方法

[英]Compiler asking for optional methods from Collections to be overridden

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

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

  • 方法addAll尽管已被添加,但抱怨它并未被覆盖,尽管它被覆盖,当我为其添加另一个addAll方法时,我也遇到了擦除错误。

我已经阅读了很多,但是找不到有关如何解决它的正确结论。 我只是使用Atom编写代码,而使用Terminal编写终端,没有花哨的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;
    }
}

事实证明,在读取API时,由于某种原因,我掩盖了addAll方法的另一个参数,这使我相信其他错误。 我更改了具有正确参数的方法,并对其进行了编译。

public boolean addAll(int index, Collection <? extends E> c)
{
    return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM