Solved

how to implement iterator without importing java.util?

Posted on 2013-11-16
10
516 Views
Last Modified: 2013-11-18
I would like to be able to use a foreach loop to iterate over my LinkedList.

This is my own implementation of a LinkedList, and does not use java.util.

Is there a way to implement an iterator without ever touching java.util?


Here is what I have so far:
public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{

@Override
	public Iterator<E> iterator() {
        return new Iterator<E>();
    }
    //iterator inner class:
	private class Iterator<E> implements MyIterator<E> { 
    	public boolean hasNext(){
    		//Returns true if the iteration has more elements.
                // TODO. for now just a STUB.
    		return true;
    	}
    	public E next(){
    		//Returns the next element in the iteration.
    		return (E) getNext();
    	}
    	public void remove(){
    		//Removes from the underlying collection the last element returned by this iterator (optional operation).
    	}
	}
// more stuff
}

Open in new window

MyIterator.java
public interface MyIterator<E>{
	boolean hasNext();
    E next();
    void remove();
}

Open in new window

(sorry for the shitty indentation, I don't know how to get the EE editor to behave)

I've tried lots and lots of permutations of what I just posted, and nothing is compiling. I'm doing me head in.
0
Comment
Question by:Kyle Hamilton
  • 5
  • 4
10 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 39653987
Your indentation is inconsistent because you're mixing spaces and tabs in your code. Make all of them one or the other and it should look better.

If I understand your question right, you're asking how to make a class that acts like the iterator that java.util.LinkedList.iterator() returns without using any of the code in the java.util package. You can do it by having your iterator keep track of the last node it returned. By doing that, implementing hasNext(), next(), and remove() should be quick.
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39654030
Thanks CPColin,

That's kind of what I did. I had implemented those methods in my DoublyLinkedList class. So then I was able to rewrite the foreach loops with a while(hasNext()), etc..

I'm using my DoublyLinkedList class as a basis for my HashTable class, where it would have been nice to be able to use the foreach syntax. I guess I thought the while() was kinda ugly inside another class.

Can you see what I'm doing wrong in the above code? any suggestions?

Thanks,
Koza.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39654068
Is there a specific problem or build error you need help with?
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39654492
error: DoublyLinkedList is not abstract and does not override abstract method iterator() in Iterable
public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{
       ^
  where E is a type-variable:
    E extends Object declared in class DoublyLinkedList
DoublyLinkedList.java:16: error: iterator() in DoublyLinkedList cannot implement iterator() in Iterable
	public MyIterator<E> iterator() {
	                     ^
  return type DoublyLinkedList<E>.MyIterator<E> is not compatible with Iterator<E>
  where E,T are type-variables:
    E extends Object declared in class DoublyLinkedList
    T extends Object declared in interface Iterable
DoublyLinkedList.java:15: error: method does not override or implement a method from a supertype
	@Override
	^

Open in new window


this is the code that throws those errors:

public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{
	
	@Override
	public MyIterator<E> iterator() {
		return new MyIterator<E>();
	}
	//iterator inner class:
	public class MyIterator<E> implements MyIteratorInt<E> { 
		public boolean hasNext(){
			//Returns true if the iteration has more elements.
			// Return hasHext() of parent class - not working

			/*  
			cannot find symbol
			return super.hasNext();
			            ^
  			symbol: method hasNext()
			*/
			return super.hasNext();
		}
		public E next(){
			//Returns the next element in the iteration.
			/*  
			cannot find symbol
			return super.getNext();
			            ^
  			symbol: method getNext()
			*/
			return super.getNext();
		}
		public void remove(){
			//Removes from the underlying collection the last element returned by this iterator (optional operation).
		}
	}
....

Open in new window

public interface MyIteratorInt<E>{
	boolean hasNext();
	E next();
	void remove();
}

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 39654716
Your MyIterator interface has to extend java.util.Iterator for you to be allowed to return it in your DoublyLinkedList.iterator() method. That method has to return something that implements java.util.Iterator, or the class doesn't fulfill the contract of java.lang.Iterable.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39654833
So there is no way to implement an iterator without java.util.Iterator?
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 39654904
There is no way to implement java.lang.Iterable, which is what enables the foreach syntax, without your iterator implementing java.util.Iterator.
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39654919
thank you.

I guess I have to figure out some other way of iterating over my List, and  worse yet, being able to remove items from my list. If you have any ideas in this regard, please let me know.

I appreciate the help.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39655482
I guess I have to figure out some other way of iterating over my List, and  worse yet, being able to remove items from my list
Just wondering why you are particularly opposed to using the java.util package containing java.util.Iterator? Why is that so different to using the java.lang package for java.lang.Iterable? (Other than the obvious, but rather trivial, requirement to put the import statement at the top of your code)
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39656179
I'm not :). Life would be a heck of a lot nicer if I could use java.util.

It's a project (that was assigned as a homework assignment once) I'm working on to learn JAVA.

(not my homework, I'm trying to learn on my own based on textbooks and other stuff, like other people's homework assignments :))
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 64
eclipse shortcuts 9 45
Windows 10 IE Certificate Issue 10 42
servlet web applications   metadata-complete="true" or false 3 3
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now