public abstract class AbstractList<E> implements Iterable<E> { public int indexOf( E elem ) { /*This instance method is supposed to find the object E in this list and return its left most occurrence in the list. If the object does not exist within the list then return -1. */ int counter = 0; while(this.next != elem) //this.next = the info part of the node in front "this" counter++; } return counter; }
What I mean to accomplish is basically to keep my pointer at node1 and check node2 for elem. If elem doesn't exist in node2 then counter goes up and until I find elem in a node counter++
Now the error I get is that next is not a value specified and is not defined for abstract.
So my question is, How can I keep my pointer at the beginning node while checking the next node in the list for elem and returning the index of the node where elem is found.
I can handle the -1 return I expect with a simple if statement.
This is my linkedlist class..
import java.util.NoSuchElementException; public class LinkedList<E> extends AbstractList<E> { private static class Node<T> { private T value; private Node<T> next; private Node( T value, Node<T> next ) { this.value = value; this.next = next; } } private Node<E> first = null; private class LinkedListIterator implements Iterator<E> { private Node<E> current; public boolean hasNext() { return ( ( ( current == null ) && ( first != null ) ) || ( ( current != null ) && ( current.next != null ) ) ); } public E next() { if ( current == null ) { current = first; } else { current = current.next; } if ( current == null ) { throw new NoSuchElementException(); } return current.value; } } public Iterator<E> Iterator() { return new LinkedListIterator(); } public int size() { Node<E> p = first; int count = 0; while ( p != null ) { p = p.next; count++; } return count; } public boolean addFirst( E e) { boolean added = false; if ( e != null ) { first = new Node<E>( e, first ); added = true; } return added; } }
Thanks in advance!
Mjall