Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

A little help with java

This topic is 3209 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
Advertisement
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
Arguably, yes. It is an intermediate access layer between private and public. I generally prefer inner classes for such things (both in Java and C++). Friend is more fine grained.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!