Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default How to run a for-each loop through an ArrayList?

    Hey guys,

    So basically I need to use a for-each loop to run through an ArrayList of animals in my "zoo," and make them print out their methods (roam(), eat(), etc).

    I've used a for loop before, but never with an ArrayList.

    Any suggestions?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: How to run a for-each loop through an ArrayList?

    Oh and I also need to include an instance of keyword at some point during this. Can I work this code into the for loop, or do I just insert it after?

    Java Code:
     
    if (animal instance of Pet) {
    System.out.println(beFriendly); 
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: How to run a for-each loop through an ArrayList?

    Use a for-each loop. If you know how to use a for-each loop then you know how to use it with an ArrayList.

    Or use a normal for loop and call get method of ArrayList.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: How to run a for-each loop through an ArrayList?

    You would need to place the if statement inside the for loop so you can apply it to each Animal in the List.

  5. #5
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: How to run a for-each loop through an ArrayList?

    So far I have:

    Java Code:
     
    Animal animal = new Animal();
    
    for (Animal animal: zooAnimals) {
    // this is the line I'm not sure of how to advance through the array list 
    int i = 0; i <= zooAnimals.size(); i++
    System.out.println(roam);
    
    if (animal instance of Pet) {
    System.out.println(play); 
    } 
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: How to run a for-each loop through an ArrayList?

    for (Animal animal: zooAnimals) {
    Above is a for-each loop.

    int i = 0; i <= zooAnimals.size(); i++
    Above is a normal for loop (missing the for keyword).

    Use one or the other. Don't try to jam both together. The for-each loop advances through the list for you. Each time around the loop the next Animal object in the List is assigned to the animal variable (btw you do not need to declare the variable before the loop. In fact you should probably get a compiler error).

  7. #7
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: How to run a for-each loop through an ArrayList?

    Okay thanks! That definitely clears things up for me

Similar Threads

  1. foreach loop on ArrayList<Stack<String>>
    By Ciwan in forum New To Java
    Replies: 21
    Last Post: 06-27-2011, 07:29 PM
  2. Problems with a loop calling data from an ArrayList.
    By moriarty in forum New To Java
    Replies: 30
    Last Post: 03-28-2010, 02:00 AM
  3. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM
  4. Replies: 0
    Last Post: 11-14-2007, 03:22 PM
  5. Replies: 0
    Last Post: 11-14-2007, 03:22 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
  •