Collections Generic Collection

devo22

Mitglied
Hi, ich soll folgende Aufgabenstellung lösen:

Write the interface for a generic collection featuring sorted insertion, deletion and an iterator (java.lang.Iterator), that provides the entries in the collection in sorted order (you do not need to implement the remove method of the iterator).

Write a sorted list class that implements your interface. Test your class, and demonstrate that the "new" form of the for loop works with your class.

Hint: The type of the entries in the collection shall be restricted to a subtype of Comparable <...>. Don't leave any types unspecified. Do not use any ready made collection class.


Hab mal mit dem Interface angefangen, aber schon hier habe ich Probleme. Das hab ich bisher:

Java:
public interface GenericCollection<E> {

	int size();
	boolean isEmpty();
	void add(E element); 				// ensures that this collection contains the specified element
	boolean contains(Object element);
	boolean remove (Object element); 	// removes a single instance of the specified element from this collection
	Iterator<E> iterator();				// returns an iterator over the elements in this collection
	boolean hasNext();					// returns true if the iteration has more elements
	Object next();						// returns the next element in the iteration

}

Was nichts anderes bedeutet, als die wichtigsten Methoden von Collection und Iterator ... ist das bisher so in etwa korrekt oder bin ich da komplett auf dem Holzweg? So ganz verstehe ich die Aufgabenstellung nämlich nicht wirklich ???:L

Im Übrigen ist mir völlig unklar, was mit "new form of the for loop" gemeint ist ???:L

Wäre für jede Hilfe dankbar!
 

Marco13

Top Contributor
Damit das mit dem neuen for-loop funktioniert
Java:
for (T t : collection) doSomethingWith(t);
muss das interface noch "Iterable<T>" extenden.
 

devo22

Mitglied
danke schon mal für die Hilfe. Das hab ich bisher:

Java:
public interface Iterator<E> {
   E next();
   boolean hasNext();
}

public interface GenericCollection<E> {

	int size();
	boolean isEmpty();
	void add(E element); 			
	boolean contains(Object element);
	boolean remove(Object element);
	Iterator<E> iterator();			

}

Java:
import java.util.*;

public abstract class SortedList<E> implements GenericCollection<E> {

LinkedList <String> list = new LinkedList<String>();

public int size() {
	return list.size();
	}

public boolean isEmpty() {
	return list.isEmpty();
	}

public void add(String element) {	
    list.add(element);
    }

public Iterator<String> iterator() {
    for (String s : list) {
    System.out.println(s);
}
}

}

Hänge momentan aber komplett, ich habe keine Idee, wie ich jetzt weitermachen soll ...
 

Neue Themen


Oben