遇到了一个小问题,即我的 DoublyLinkedListlastIndexOf(E element)
仅返回 -1 的位置。我的代码indexOf(E element)
虽然可以正常工作。
我用我DoublyLinkedListIterator
的来做这个发现。目前的代码是:
/**
* Returns an element of type E
*/
@Override
public Iterator<E> iterator() {
return new DoublyLinkedListIterator();
}
/**
* An Iterator Class for DoublyLinkedLists
*/
private class DoublyLinkedListIterator implements Iterator<E>{
private Node current = node.next; // The node to be returned by next();
private Node lastUsed = null; // The last node to be returned by either previous() or next()
// This will be resetted when remove() is used or if add() is used.
private int index = 0; // Keeps track of index for use in next() and previous(), add() and remove()
public boolean hasNext() {
return index < numberOfElementsInList; // if index is less than the number of elements in the DoublyLinkedList, return true
}
public E next() {
if(hasNext() == false){
throw new NoSuchElementException();
}
lastUsed = current; // Makes the current node and remembers that it was last used
// Remember, current is already
E data = current.data; // Adds the element in current node to E data
current = current.next; // makes the next node after the current node, THE current node.
index++;
return data; // return that data
}
public boolean hasPrevious() {
return index > 0; // if the index is greater than 0, then there is at least 1 element in the DoublyLinkedList. Return true if there is.
}
public E previous() {
if(hasPrevious() == false){
throw new NoSuchElementException(); // if hasPrevious() returns false (meaning if there is no previous node), will throw exception.
}
current = current.prev; // Takes the previous Node
index--;
lastUsed = current; // Will set this Node to lastUsed, so that way if remove() or add() is used, it won't throw an exception.
return current.data;
}
这就是我目前的 indexOf() 和 lastIndexOf():
@Override
public int indexOf(E element) {
DoublyLinkedListIterator indexOfIterator = new DoublyLinkedListIterator();
int index = 0;
while(indexOfIterator.hasNext()){
if(indexOfIterator.next().equals(element)){
return index;
}
index++;
}
return -1;
}
/**
* Returns the index with this element of it's last occurrence.
* Returns -1 if the element is not found.
*/
@Override
public int lastIndexOf(E element) {
DoublyLinkedListIterator lastIndexIterator = new DoublyLinkedListIterator();
int index = numberOfElementsInList;
while(lastIndexIterator.hasPrevious()){
if(lastIndexIterator.previous().equals(element)){
return index;
}
index--;
}
return -1;
}
我检查它的方式如下:
public static void main(String[] args){
DoublyLinkedList<Object> list = new DoublyLinkedList<>();
for(int i = 0; i < 10; i++){
list.add(i, i+1);
}
list.add(8, 3);
printArray(list.toArray());
System.out.println("The number 2 shows up first first at index: " + list.indexOf(3));
System.out.println("The number 2 shows up very last at index: " + list.lastIndexOf(3));
}
这给出了以下结果:
1 2 3 4 5 6 7 8 3 9 10
The number 2 shows up first first at index: 2
The number 2 shows up very last at index: -1
我在这里做错了什么?我认为它就像 indexOf() 一样,但只是通过从它的 size() 中减少索引来向后退一点。