Ran into a slight problem of where for my DoublyLinkedList's lastIndexOf(E element)
only returns -1. My code for indexOf(E element)
works correctly though.
I'm using my DoublyLinkedListIterator
to do the finding. The code for that is currently:
/**
* 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;
}
And this is what I currently have for my indexOf() and 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;
}
The way that I'm checking it is by the following:
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));
}
Which gives out the following results:
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
What am I doing wrong here? I figured it would be just like indexOf() but only a bit backwards by decrementing the index from it's size().