尝试添加时,如果isFull()
为真,则需要调整为两倍大小。
public boolean add(T element) {
if (this.contains(element)) {
return false;
}
if (isFull()) {
resize(elements.length * 2);
}
//etc,etc.
}
//check if full
private boolean isFull() {
if (size == elements.length) {
return true;
}
return false;
}
//resize
private void resize(int capacity)
T[] a = Arrays.<T>copyOf(elements, capacity);
elements = a;
}
但是,我的调整大小方法出错。
Error:ArraySet.java:114: error: cannot find symbol
T[] a = Arrays.<T>copyOf(elements, capacity);
^
symbol: variable Arrays
location: class ArraySet<T>
where T is a type-variable:
T extends Comparable<? super T> declared in class ArraySet
1 error
我做错了什么?我可以做些什么来调整大小?我该如何调整大小?
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArraySet<T extends Comparable<? super T>> implements Set<T> {
T[] elements;
int size;
@SuppressWarnings("unchecked")
public ArraySet() {
elements = (T[]) new Comparable[1];
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
........这就足够了吗?