Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating an iterator for my class

thatos's Avatar
Member
 
Join Date: Aug 2007
Location: SOUTH AFRICA
Posts: 91
#1: Jun 12 '08
I designed a class called Row. This class has the following variable
Expand|Select|Wrap|Line Numbers
  1.     private String country,city;
  2.     private String connections[];
  3.     private boolean sea_link;
  4.     private int cumul,section;
  5.  
I would like to write an iterator method which goes like this
Expand|Select|Wrap|Line Numbers
  1.     public Iterator<Row> iterator(){
  2.           return iterator;
  3.     }
  4.  
But I do not know how to go about doing this.Can someone pls help me, I do not know where to start.

thatos's Avatar
Member
 
Join Date: Aug 2007
Location: SOUTH AFRICA
Posts: 91
#2: Jun 12 '08

re: Creating an iterator for my class


I have the following contructor in my class
Expand|Select|Wrap|Line Numbers
  1.   public Row(String country, String city,  int distPred, int distStart, String connections[]) {
  2.  
  3.       this.country = country;
  4.       this.city = city;
  5.       cumul = distStart;
  6.       section = distPred;
  7.       this.connections = connections;
  8.  
The class Row records each row in a table
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Jun 12 '08

re: Creating an iterator for my class


Do you want to iterate over the connections? If not you can't iterate over one single row.

kind regards,

Jos
thatos's Avatar
Member
 
Join Date: Aug 2007
Location: SOUTH AFRICA
Posts: 91
#4: Jun 12 '08

re: Creating an iterator for my class


Quote:

Originally Posted by JosAH

Do you want to iterate over the connections? If not you can't iterate over one single row.

kind regards,

Jos

The is another class Table which is a collection of Rows and it implements Iterable<Row>, it has to contain this method
Expand|Select|Wrap|Line Numbers
  1. public Iterator<Row> iterator() {
  2.  
  3.         return rows.iterator();
  4.  
  5.       }
  6.  
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jun 12 '08

re: Creating an iterator for my class


Quote:

Originally Posted by thatos

The is another class Table which is a collection of Rows and it implements Iterable<Row>, it has to contain this method

Expand|Select|Wrap|Line Numbers
  1. public Iterator<Row> iterator() {
  2.  
  3.         return rows.iterator();
  4.  
  5.       }
  6.  

If the "other class" is a real collection then you might already have your iterator.
Please elaborate.

kind regards,

Jos
thatos's Avatar
Member
 
Join Date: Aug 2007
Location: SOUTH AFRICA
Posts: 91
#6: Jun 12 '08

re: Creating an iterator for my class


Quote:

Originally Posted by JosAH

If the "other class" is a real collection then you might already have your iterator.
Please elaborate.

kind regards,

Jos

The other class is not a "real" collection.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Jun 12 '08

re: Creating an iterator for my class


Quote:

Originally Posted by thatos

The other class is not a "real" collection.

Well then you have to duly implement the Iterator interface for that class. Read
the API documentation for the interface. hasNext() and next() are the two
methods that you have to implement yourself for your home brew collection class.
An inner class would do fine.

kind regards,

Jos
Reply