Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: stuck on Iterator

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default stuck on Iterator

    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


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: stuck on Iterator

    Suppose root is your pointer that is pointing to the first node, you may have two possible solutions.
    1. If you only want to check immediate node only, do root.getnext().getvalue().
    2. If you want to keep your root pointer at the start and want to check all nodes, take a temporary pointer and first assign it the root's address and then in loop check for values until it gets null or it's next gets null.

Similar Threads

  1. iterator help needed
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 12:32 PM
  2. [SOLVED] Iterator next import class?
    By daydreamer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 04:51 AM
  3. Iterator Problem
    By chrisych in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 10th, 2010, 01:29 PM
  4. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM
  5. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM