Solved

how to implement iterator without importing java.util?

Posted on 2013-11-16
10
537 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 74
hibernate example for saving data 19 77
null output 3 42
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 64
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 …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

733 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