?
Solved

how to implement iterator without importing java.util?

Posted on 2013-11-16
10
Medium Priority
?
606 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 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