我和学校伙伴一起创建了一个全班级的项目,我应该为某些功能创建拉取请求,但是我在创建类和覆盖方法时遇到了一些问题它只是编译的方式(我现在不需要编写方法,这就是项目,我只需要编译)。我发现大多数方法都有效(或至少编译器没有抱怨),但我对一些事情感到困惑:
编译器抱怨可选方法(如set和addAll)
方法addAll虽然已经添加,但是抱怨它没有被覆盖,尽管它已经被覆盖了,当我为它添加另一个addAll方法时,我也得到了一个擦除错误。 / p>
我已经读了很多关于它的内容,但我无法就如何解决它找到一个正确的结论。我只是使用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;
}
}
答案 0 :(得分:0)
事实证明,在阅读API时,由于某种原因,我对addAll方法的其他参数进行了掩饰,这让我相信其他错误。我使用正确的参数更改了方法并进行了编译。
public boolean addAll(int index, Collection <? extends E> c)
{
return true;
}