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

    Default iterator help + Arraylist

    i have an ArrayList<Fish> fish. I was wondering how to
    use the iterator method. I want to go through each element
    in the arraylist and do the following:

    isAlive is a boolean method that checks if the fist is size 0 or not. If it is then it returns true.
    shrink method just takes the size of a fish and decrease is by two.

    I have written these methods already.

    I was wondering how to write the iterator/ use iterator...im new to this.. I know in arrays you
    had to create a for loop and iterate through each element that way.

    Thanks for help.


    Java Code:
    	/** Iterates through fish list.  For each fish that isAlive, shrinks the fish by
    	 * invoking it's "shrink" method. */
    	public void shrinkFish() {
    		throw new RuntimeException("You code this!");
    			
    		}
    	}

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: iterator help + Arraylist

    you need to use a for each loop for your arrayList, i don't now what the isAlive is but i assume a boolean variable. So in this case you need to:

    1, do a for each loop through the ArrayList<Fish>
    2, check to see if the current Fish is alive
    3, if it is call the shrink method
    4, if it isnt alive do nothing

    steps 3,4 can be handled using the same if statement.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iterator help + Arraylist

    Since ArrayList also implements iterable (which means you can get an iterator via the iterator() method) you can use the shorthand form of iteration:

    Java Code:
    for (Fish f : fish) {
    // do something with f
    }
    You can also do the following:

    Java Code:
    Iterator<Fish> it = fish.iterator();
    Fish f;
    while (it.hasNext()) {
        f = it.next();
       // do something with f
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: iterator help + Arraylist

    Thank you all. I wasn't sure how to implement the syntax part. Thanks

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

    Default Re: iterator help + Arraylist

    isAlive method is a Boolean method...it returns true if the fish size is greater than zero..

    How do i go about checking each element is alive??

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

    Default Re: iterator help + Arraylist

    Nevermind. i figured it out!

Similar Threads

  1. Replies: 7
    Last Post: 12-28-2011, 10:25 AM
  2. DIFFERENCE BETWEEN ITERATOR AND iterator( )
    By mark clarke in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 09:41 AM
  3. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  4. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 02:12 PM
  5. Replies: 0
    Last Post: 05-18-2010, 12:53 AM

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
  •