Sign in to follow this  

A little help with java

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is just an exercise. I am trying to create a general Tree to learn more about them and their recursive nature. I wanted to create a double linked to use in the general tree. Right now I am kind of confused when implementing the iterator for the double linked list. This is the simple iterator class:
package DoubleLinkedList;

import java.util.Iterator;

public class ListIterator<T> implements Iterator<T>
{
	private DList<T> list;
	
	private DNode<T> currPos;
	
	public ListIterator(){		
	}
	public ListIterator(DList<T> List, DNode<T> start){			
		list = List;
		currPos = start;
	}
	@Override
	public boolean hasNext() {
		return currPos.nextNode() == null;
	}

	@Override
	public T next() {
		T val = currPos.getElement();
		currPos = currPos.nextNode();
		return val;
	}

	@Override
	public void remove() {
		throw new java.lang.NoSuchMethodError("Method remove() not supported");
	}

}



I wanted to use it in the insert method in the double linked list, specifically here ( which is empty ):
public void insert(ListIterator<Type> pos) {

}


The only way the client will get a hold of the iterator for a double linked list is from this method :
public ListIterator<Type> iterator(){
	return new ListIterator<Type>(this,_head);
}


The main problem is, that in the insert method I wan't to check if the iterator is for this double linked list. One way I think could achieve this is to create a member variable ListIterator<Type>(this). I am not sure though. Any suggestions please?

Share this post


Link to post
Share on other sites
If the ListIterator is in the same package as your container, you take advantage of the fact that protected access grants package classes access to the member. So if DList<T> where protected instead of private, then in Dlist#insert you could check if pos.list == this to determine if the iterator belongs to the list.

Alternatively, you could implement your iterator as an inner class, which means that the two classes can access each other.

Stylistically, if you go with package protected access, I would mark the "list" field as final, so that other classes in the package won't accidentally assign to it.

Share this post


Link to post
Share on other sites
I should warn you that such techniques introduce heavy coupling between the classes. You should avoid this whereever possible. How an iterator is implemented is typically heavily tied to the implementation of the data structure (iterators tend to require access to what are conceptually private members) so the coupling is justified.

In general though, it is rarely necessary to expose members like this. The result will be brittle code that is hard to read, reason about or extend.

Share this post


Link to post
Share on other sites

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this