ich diesen einfachen CodeIterator Generika, nicht einen Iterator der Typen unpassende (vielleicht Casting Ausgabe) erklären
public String toString() {
**Iterator it = list.iterator();**
String s;
while(it.hasNext()){
s = s + " " + Integer.toString(it.next());
}
System.out.println(s);
// IMPLEMENT THIS METHOD VIA AN ITERATOR
// MAKE SURE THE OUTPUT PRODUCED BY THE METHOD
// DISPLAYS THE LEAST SIGNIFICANT BIT TO THE RIGHT.
}
Auf der Linie mit Sternchen haben, bekomme ich folgende Kompilierungsfehler.
Error: incompatible types
required: Iterator
found: java.util.Iterator<java.lang.Integer>
ich dies bereits versucht,
**Iterator<Integer> it = list.iterator();**
ich, Error: type Iterator does not take parameters
EDIT *
ich vergaß zu erwähnen, ich meine eigene Implementierung der Interface-Methoden haben.
/*
* Inner class to implement the iterator for the <code>BitList</code>.
*/
private class BitListIterator implements Iterator {
/*
* A reference to the current <code>Node</code> in the iterator.
*/
private Node current = null;
/*
* The expected modification count variable.
* Needed for the "fast-fail" implementation of the iterator.
*/
private int expectedModCount = modCount;
/*
* Advances the iterator to the next element in the list.
*/
public int next() {
checkValid();
if (current == null) {
current = first ;
} else {
current = current.next ; // move the cursor forward
}
if (current == null)
throw new NoSuchElementException() ;
return current.value ;
}
/**
* Inserts a new <code>int</code> value immediately before the next element that would be returned by <code>next()</code>.
*/
public void add(int newElement) {
Node newNode;
if (current == null) {
first = new Node(newElement, first);
current = first;
} else {
current.next = new Node(newElement, current.next);
current = current.next;
}
modCount++ ;
expectedModCount++ ;
}
/**
* Indicates whether there is a next element in the list being traversed or not.
*/
public boolean hasNext() {
return ((current == null) && (first != null)) ||
((current != null) && (current.next !=null));
}
/**
* Checks whether this iterator remains valid, i.e. no concurrent modifications have been made on the list.
*/
private void checkValid() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
}
} // end of BitListIterator class
Also, wenn Import das Paket erhalte ich die folgenden Fehler
Error: BitList.BitListIterator is not abstract and does not override abstract method remove() in java.util.Iterator
Error: next() in BitList.BitListIterator cannot implement next() in java.util.Iterator
return type int is not compatible with java.lang.Object
Ich habe JDK 1.7 und seine im Einsatz.
Irgendwelche Ideen würden sicherlich helfen.
Danke,
Mjall2
Der Compiler ist korrekt. Sie haben 'remove()' nicht implementiert und Ihre Definition von 'next()' ist nicht kompatibel mit dem 'next()', das in der 'Iterator'-Schnittstelle definiert ist. Sie müssen diese Probleme beheben, wenn Sie Ihre Iterator-Klasse verwenden möchten. – QuantumMechanic
Welcher Typ ist 'liste'? Ist es "BitList"? Versuchen Sie, dass Ihre 'BitList'-Klasse Ihre' BitListIterator'-Implementierung zurückgibt, wenn 'iterator()' aufgerufen wird? – QuantumMechanic
stellen Sie sicher, dass Sie keine Klassendatei namens Iterator.class im selben Verzeichnis haben – newacct