Results 1 to 6 of 6
- 03-23-2011, 12:28 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Java newbie question on linkedlist index search
Hi guys,
I am new here and signed up for the purpose of gaining and if I can distributing knowledge.
I have a very specific question as to this piece of code I have developed,
Java Code: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. */ } }Java Code:public interface Iterable<T> { public abstract Iterator<T> iterator(); }This is my linked list class ...Java Code:public interface Iterator<T> { public abstract boolean hasNext(); public abstract T next(); }
Any help at all will be highly appreciated and I will remain forever thankful!Java Code: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; } }
Mjall
- 03-23-2011, 12:44 AM #2
Help with what? Ask a specific question and get a specific answer.
- 03-23-2011, 04:36 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
True, I lost focus my bad.
The question is
I would like to in the AbstractFinal class, be able to implement the indexOf() method.
The description for the method is that it receives indexOf(E elem) and what the method is supposed to return is the index of the left most occurrence of the object elem in the linkedlist.
If I can clarify more please let me know.
Thanks
Mjall
- 03-23-2011, 06:03 AM #4
Still no question. All you have done is restate your requirements. "How do I write the indexOf method?" is a question but not specific. Make an attempt at writing the code, then post your attempt here and explain what is wrong with it (errors, incorrect behaviour). Once again a specific question will get you a specific answer.
- 03-24-2011, 05:06 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Ok so what I did was
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++Java Code: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; }
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.
Thanks
Mjall
- 03-25-2011, 10:54 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Problem with search, when I rewriting index
By Petr in forum LuceneReplies: 0Last Post: 01-13-2011, 10:58 AM -
How to search russian texts in Lucene index?
By ArtUrlWWW in forum LuceneReplies: 0Last Post: 01-11-2011, 11:46 AM -
Problems with javadoc Index search.
By heven in forum NetBeansReplies: 0Last Post: 08-09-2010, 10:46 AM -
problem with index file search
By rencin in forum LuceneReplies: 4Last Post: 08-17-2009, 10:27 AM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks