Вопрос

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 T or E as 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:

class Bag<E> implements java.util.Collection<E> 

You will also need to either return values or throw an exception for those methods that don't return void.

public int size() {
    throw UnsupportedOperationException();
}

Другие советы

E should be T, which is the generic type you are going to store. Your constructor should be for your class not the interface. Also you will need to add at least stubbed returns in your methods that have a return value, e.g. "return false" if the return is a boolean.

Hope this helps.

Try changing the import statement to import java.util.*;

Because you don't have E declared , use T instead and also you need to return wherever it is declared to return.

You are gonna have to do a lot of basic learning before you can implement an interface in JAVA. May be read "thinking in java" or something In you program there are multiple problems

  1. import of the Collection class is missing
  2. no return statements are there in any of the methods
  3. replace the E with T

Your generic parameter is called T and you're using E in your methods' implementations. If you go with T as the element type, update the method signatures.

It probably still won't compile without at least dummy implementations such as return null; or return false; in non-void methods.

The java.util.Iterator is an interface that describes a set of methods which allow you to iterate over the elements in your collection. You would return an instance of a class that implements that interface and its methods. YourListIterator will probably keep a reference or index to where in the list you are, return the corresponding element and move forward through the list.

Generics (including the use of ?) are discussed in this tutorial.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top