Solved

how to implement iterator without importing java.util?

Posted on 2013-11-16
10
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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 36

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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