Creating an iterator for my class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    Creating an iterator for my class

    I designed a class called Row. This class has the following variable
    Code:
    	private String country,city;
    	private String connections[];
    	private boolean sea_link;
    	private int cumul,section;
    I would like to write an iterator method which goes like this
    Code:
    	public Iterator<Row> iterator(){
    	      return iterator;
    	}
    But I do not know how to go about doing this.Can someone pls help me, I do not know where to start.
  • thatos
    New Member
    • Aug 2007
    • 105

    #2
    I have the following contructor in my class
    Code:
      public Row(String country, String city,  int distPred, int distStart, String connections[]) {
    	  
    	  this.country = country;
    	  this.city = city;
    	  cumul = distStart;
    	  section = distPred;
    	  this.connections = connections;
    The class Row records each row in a table

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

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

      kind regards,

      Jos

      Comment

      • thatos
        New Member
        • Aug 2007
        • 105

        #4
        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
        Code:
        public Iterator<Row> iterator() {
        
        	    return rows.iterator();
        
        	  }

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          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
          Code:
          public Iterator<Row> iterator() {
          
          	    return rows.iterator();
          
          	  }
          If the "other class" is a real collection then you might already have your iterator.
          Please elaborate.

          kind regards,

          Jos

          Comment

          • thatos
            New Member
            • Aug 2007
            • 105

            #6
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

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

              Comment

              Working...