I need to implement a bag data structure using the interface java.util.Collection.
I’m not asking for help on the actual implementation of the data structure. I just can’t get my program to compile. I just want to get a blank implementation of the interface (with non functional signatures of the methods) to compile before I start actually implementing methods.
class Bag<T> implements java.util.Collection<T>
{
public void Collection () {
}
public boolean add(E e) {
}
public boolean addAll (Collection<? extends E> c) {
}
public void clear() {
}
public boolean contains(Object o) {
}
public boolean containsAll(Collection<?> c) {
}
public boolean equals(Object o) {
}
public int hashCode() {
}
public boolean isEmpty() {
}
public Interator<E> interator() {
}
public boolean remove(Object o) {
}
public boolean removeAll(Collection<?> c) {
}
public int size() {
}
public Object[] toArray() {
}
public <T> T[] toArray(T[] a) {
}
}
Compiler can’t find class E in the parameters of methods like add. Am I supposed to define a class for E, or is there something I’m not understanding about what E actually is? Compiler says it can’t find class Collection (in the parameters of methods like addAll) Do I import java.util.Collection or is there something else I should know? Compiler also has no idea about class Iterator and neither do I.
I know this is all probably elementary, but I could not find anything via Google, etc yesterday and my professor’s lectures don’t follow the projects at all. I’m lost on this one. Thanks for any help!
Edit: Also, I haven’t searched as much on this but if someone could tell me anything useful about the “?”s such as public boolean addAll (Collection<? extends E> c) {}, that would be greatly appreciated.
Either use
TorEas the type parameter, and then use it consistently throughout your class definition.The Java documentation uses
E, so you could for example change the first line to:You will also need to either return values or throw an exception for those methods that don’t return void.