Solved

how to implement iterator without importing java.util?

Posted on 2013-11-16
10
509 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

17 Experts available now in Live!

Get 1:1 Help Now