d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iterator Question
Add Reply New Topic New Poll
Member
Posts: 3,193
Joined: Apr 28 2010
Gold: 242.05
Dec 1 2013 12:37pm
Im making a graph class and we have to implement iterators over edge and vertex of the graph.
My question is can you have 2 unique iterator for 1 class?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2013 12:43pm
you can make as many different iterators as you want. have fun!
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Dec 1 2013 03:38pm
Quote (Naiva @ Dec 1 2013 02:37pm)
Im making a graph class and we have to implement iterators over edge and vertex of the graph.
My question is can you have 2 unique iterator for 1 class?


Are these subclasses of your graph class?

You can make a million iterators for whatever you want to put in a data structure, but if you want to create your own data structure of edge and vertex and make them iterateable, you have to
Code
implements Iterable
on those classes
Member
Posts: 3,193
Joined: Apr 28 2010
Gold: 242.05
Dec 1 2013 04:06pm
Quote (carteblanche @ Dec 1 2013 01:43pm)
you can make as many different iterators as you want. have fun!


thnx for response I'm still a little confused, if say I have a graph class i.e

Code
public class Graph<E> implements Iterable<E>{
public Iterator<E> iterator(){
return new edgeIterator();
}
public edgeIterator()....
public vertexIterator()...
}


what if I want to return a vertexIterator instread? I'm assuming I can't make 2 iterator methods.
Quote (labatymo @ Dec 1 2013 04:38pm)
Are these subclasses of your graph class?

You can make a million iterators for whatever you want to put in a data structure, but if you want to create your own data structure of edge and vertex and make them iterateable, you have to
Code
implements Iterable
on those classes


Yea I'm writing my own vertex/edge class and trying to make it so that the graph stores the vertex/edge objects
so would I make the graph class iterable?

This post was edited by Naiva on Dec 1 2013 04:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2013 04:15pm
Quote (Naiva @ Dec 1 2013 05:06pm)
thnx for response I'm still a little confused, if say I have a graph class i.e

Code
public class Graph<E> implements Iterable<E>{
  public Iterator<E> iterator(){
return new edgeIterator();
  }
}


what if I want to return a vertexIterator instread?  I'm assuming I can't make 2 iterator methods.

Yea I'm writing my own vertex/edge class and trying to make it so that the graph stores the vertex/edge objects
so would I make the graph class iterable.?


why not?

perfectly fine to have getEdgeIterator() and getNodeIterator()

if your assignment's instructions say you can't use a second iterator method, then you need to set a flag on your object. eg: setIteratorType(edge or node)

This post was edited by carteblanche on Dec 1 2013 04:15pm
Member
Posts: 3,193
Joined: Apr 28 2010
Gold: 242.05
Dec 1 2013 04:25pm
Quote (carteblanche @ Dec 1 2013 05:15pm)
why not?

perfectly fine to have getEdgeIterator() and getNodeIterator()

if your assignment's instructions say you can't use a second iterator method, then you need to set a flag on your object. eg: setIteratorType(edge or node)


ah kk i see now
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll