Results 1 to 2 of 2
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Real life example of iteration

    I need a little help with an assignment, to come up with a real-world example of iteration, and how it can be achieved recursively.

    I was thinking of someone looking for something inside of a box, maybe a specific item:


    Java Code:
    public boolean containsItem(){
    Iterator itr = Box.contents().iterator;
    Object myItem = paper;
    while(itr.hasNext()){
    item i = itr.next();
    
    if (i == myItem){
    return i;
    }
    } 
    
    return false
    
    }
    Would you consider this to be a good real iteration example?
    Last edited by fam2315; 06-19-2011 at 08:13 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    One problem is the code shouldn't compile. One return is of an int another of an item.
    Another is that the item to be tested for should be passed to the method vs created in the method.

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2011, 10:25 PM
  2. Two Dimensional Array Iteration Help
    By Stamoulohta in forum New To Java
    Replies: 5
    Last Post: 03-13-2011, 01:39 PM
  3. Double Iteration Over a Hashmap
    By Gusy in forum New To Java
    Replies: 1
    Last Post: 12-01-2010, 01:01 AM
  4. Arrays, Iteration and selection (Cache)
    By dietgal in forum New To Java
    Replies: 17
    Last Post: 10-11-2008, 07:44 AM
  5. Enum Iteration
    By A.Russell in forum New To Java
    Replies: 1
    Last Post: 08-15-2007, 12:17 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
  •