Results 1 to 15 of 15

Thread: Iterator help

  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Iterator help

    Hello. I need to implement my own iterator and I am getting bad results. I was wondering if I can ask for some help.
    So basically I have a Food class that holds 10 cheese objects and I need to make an iterator that goes through Food and prints each cheese in the food class. But its not coming out the way its supposed to. Thanks in advance

    Java Code:
    package iterator;
    
    import java.util.Iterator;
    
    public class FoodIterator extends Food implements Iterator<Cheese> {
    
    
    	private Food[] foodList;
    	private int numCheese = 0;
    	private Food z;
    
    	public FoodIterator (Food Object){
    		this.z = Object;
    	}
    
    	@Override
    	public boolean hasNext() {
    		if (this.numCheese < 10) return true;
    		else return false;
    	}
    
    	@Override
    	public Food next() {
    		Food foodObject = FoodList[numCheese];
    		numCheese++;
    		return foodObject;
    	}
    
    	@Override
    	public void remove() {
    		//Empty
    	}
    }
    
    
    //-----------------------------------
    
    
    package iterator;
    
    public class Food implements Iterable<Cheese>{
    
    	
    	private Food[] foodList;
    	private int numCheese;
    
    	public Food() {
    		foodList= new Food[10];
    		numCheese= 0;
    	}
    
    	
    	public void addCheese(Cheese c) {
    		foodList[numCheese++]= c;
    	}
    
    	public FoodIterator iterator(){
    		return new FoodIterator(this);
    	}
    
    }
    Last edited by NoobieCode; 09-20-2013 at 11:12 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Iterator help

    so where exactly are you filling the foodList array of your FoodIterator class?

    And the code you posted doesn't even compile.

    Java Code:
    Food foodObject = FoodList[numCheese]; // FoodList in stead of foodList ?
    Post the actual code you are using.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    sorry for that. Super late at night.

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    Java Code:
    package iterator;
    
    
    public class Food implements Iterable<Cheese>{
    
    	
    	private Cheese[] List;
    	private int num;
    
    	public Food() {
    		List= new Cheese[10];
    		num= 0;
    	}
    
    	
    	public void addCheese(Cheese c) {
    		List[num++]= c;
    	}
    
    	public FoodIterator iterator(){
    		return new FoodIterator(this);
    	}
    
    }
    
    //-----------------
    
    package iterator;
    
    import java.util.Iterator;
    
    public class Iterator implements Iterator<Cheese> {
    
    	
    	private Cheese[] List;
    	private int num = 0;
    	private Food f;
    
    	public Iterator (Food Object){
    		this.f = Object;
    	}
    
    	@Override
    	public boolean hasNext() {
    		if (num < 10) return true;
    		else return false;
    	}
    
    	@Override
    	public Cheese next() {
    		Cheese Object = List[num];
    		num++;
    		return Object;
    	}
    
    	@Override
    	public void remove() {
    		//Empty
    	}
    }
    Last edited by NoobieCode; 09-20-2013 at 12:23 PM.

  5. #5
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    There are other classes like what makes up the cheese object. I was wondering if I could get some advice on making this iterator work. It giving me null pointer exceptions around my publice Cheese next() method. I don't know what I am doing wrong.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Iterator help

    Apparently you read only half of my previous reply. Lets try again in a different form. Why do both Food AND Iterator have a List array?

    And btw: don't use variable names starting with a capital, use proper Java naming conventions. You are creating highly confusing code here since there is also an interface called List. Class names start with a capital, variable names do not.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    I dont even know what im doing anymore.There's a cheese class that stores info about cheese and theres a Food class that stores cheese objects. (up to 10) using an array. I have implement a method called iterator in the Food class. I have to first override the iterator in a new class. I was just wondering how to do that.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Iterator help

    Quote Originally Posted by NoobieCode View Post
    I dont even know what im doing anymore.
    Fix that first, and that is something you have to do. You've created a mess, probably pressure of time is making you push forward and you are losing focus. Stop that. Start over, start fresh and think about it new.

    - you have a class with an array in it; lets call it Names
    - you want to have ANOTHER CLASS NameIterator (not necessarily a subclass, just another class) that can iterate over that array of names in the Names class

    If you put it like that, wouldn't that mean that NameIterator needs access to the array of names that is held by the Names class?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    so i have to make a constructor that passes Names into the parameter

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Iterator help

    If you look at the existing Collections framework and dig into the code, you'll see that the Iterator classes associated with a particular type of Collection (eg AbstractList) are inner classes. This gives them access to the underlying array of their associated collection without exposing that array (via a getter for example) to the outside world. They do not hold a copy of the array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Iterator help

    How would you implement the next() method in Iterator. I can't seem to put it into code. For hasNext, the index has to be less than the array length, itll return true. For next(), I don't know how to say, If hasNext is false, stop, if not, then the next item is array[index ++]

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Iterator help

    The API for Iterator says what you are supposed to do when there are no more elements:
    "
    NoSuchElementException - if the iteration has no more elements
    "
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Iterator help

    The next() doesn't have to stop or even care about hasNext() - that is part of the API interface you provide to the programmer using it. The existing Iterator in the JDK also works like that, you must use it like:

    Java Code:
    Iterator<SomeClass> it = collection.iterator();
    
    while(it.hasNext()){
    
      SomeClass obj = it.next();
      ...
    }
    If you try to use next() when there is nothing next, you'll get an exception. Rightfully so, because you created a bug in your code when you try to do that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Iterator help

    Oh, and note that the implications of next() and remove() are that your pointer (the 'num' index) should be pointing at the current element, not the next one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Iterator help

    I'm ducking out here, it does not help when two people start to pass hints from different perspectives :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. DIFFERENCE BETWEEN ITERATOR AND iterator( )
    By mark clarke in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 10:41 AM
  2. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 01:17 PM
  3. Replies: 0
    Last Post: 05-18-2010, 01:53 AM
  4. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 02:32 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 03:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •