Results 1 to 5 of 5

Thread: Iterator help

  1. #1
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Iterator help

    Hey. Starting to work on a project I got due for a class on Friday. We are to use Iterators to cycle through ArrayLists to look for certain things. In the code I have posted below, we are supposed to be checking if an object with a specific String exists, and if not, then add it to the ArrayList. I've spent a few hours messing around with it and reading up in my book, but I know I am missing something here. I am fairly certain BinaryTree should be some other data type, but that is what is given as an example in my book so at this point I am not sure what else I could put there. Eclipse doesn't like inorderIterator() either, but that seemed to be the one to use when traversing an ArrayList, so not sure what's wrong there either. The other thing I think I am doing wrong is invoking the getType() method from the Event() class (line with the comment: //checks if type t already exists). If I could figure out what I am doing wrong here, the rest of this project shouldn't be too difficult.

    Java Code:
    import java.util.List;
    
    
    public class EventDB {
    	java.util.ArrayList events = new java.util.ArrayList(); //list of events
    	
    	public EventDB(){
    		events.add(new Event(null)); //create a generic EventDB object
    	}
    	
    
    		/**
    		 * Add an event with the given type t to the end of the database. If an event with *the type t is already in the database, just return.
    		 */
    	public void addEvent(String t){
    		
    		BinaryTree<java.util.ArrayList> temp = new BinaryTree<java.util.ArrayList>();
    		
    		java.util.Iterator iterator = events.inorderIterator();
    		while (iterator.hasNext()){ //loop to check if t exists
    			if (iterator.getType() == t){ //checks if type t already exists
    				return; 
    			}
    		}
    		events.add(new Event(t)); //adds new event to database
    	}
    }
    The methods for Event (class was already written and given with assignment) are as follows:

    Java Code:
    Event(String) 
    getType(): String
    getRoster(): List<String>
    Please let me know if there is any more information I can provide.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Iterator help

    What is a BinaryTree? Is it a proprietary class? If so, check its API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Iterator help

    I thought BinaryTree was a data type used for iterators to function, but it looks like it was a class from a previous chapter in my book that has no bearing on this project what so ever. Perhaps I don't even need that line of code at all, or there is supposed to be something else in place of BinaryTree. I am still not quite sure how to implement the iterator in my code.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Iterator help

    Have you done generics?
    Because that ArrayList should be an ArrayList<Event>, which would make your iterator code easier.

    Anyway, just iterate and in each loop check the value of type.
    You need to use the result of Iterator.next(), which is the actual Event object.
    Note you also need to use the equals() method, not '=='.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Iterator help

    Thanks for the input. I figured ArrayList<Event> was something I was missing. Glad to hear some feedback that is in line with that thought process.

    So Iterator.next().getType() should get me the string from the Event() object that the current Iterator is on? In a couple hours I will have a chance to work on it and try it out.

    Generics is something that we worked on, but not something that I got a lot of experience working with. Unfortunately I had missed some classes due to a death in the family, so I am currently playing catch up.

    Thanks again!

Similar Threads

  1. DIFFERENCE BETWEEN ITERATOR AND iterator( )
    By mark clarke in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 09:41 AM
  2. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 12:17 PM
  3. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 02:12 PM
  4. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  5. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM

Posting Permissions

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