When trying to add, if isFull()
is true then need to resize to twice the size.
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;
}
However, there is an error with my resize method.
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
What am I doing wrong? What can I do to resize? How do I resize?
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;
}
........ is this enough of the code?