Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Loop through a 2d array of objects

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop through a 2d array of objects

    Hi!

    I was wondering if anyone can tell me a statement to iterate through a 2d array of objects called "animalsAndGrass".
    While it is iterating through it I want to be able to output the open locations i.e. the locations in the 2d array not holding any objects.

    For example i know i can iterate through the array like so..
               for (int i = 0; i < ROWS; i++)
                       {
    			for (int j = 0; j < COLS; j++)
                            {
     
                            }
                       }

    Would I be better to output and store the open locations to another 2d array? or what should I do?
    I'm so confused

    extra..
    this is for my dissertation I need to loop through the array and..
    If it is an empty cell in the grid (2d array)
    and 3 or more neighbouring cells are a grass object then create a grass object and place it in the empty space

    Thank you very much for your help.
    Last edited by ssjg0ten5; March 28th, 2012 at 06:50 PM.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    117
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Loop through a 2d array of objects

    If you know how to iterate through the array then it is a simple matter of checking if the cell has an object in it or not. A simple if statement checking the cell (based on i and j) and outputting something like "has object" and "no object" would be a good place to start and go from there.

Similar Threads

  1. Help with storing objects in array? :(
    By spoonemore11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 2nd, 2011, 04:28 PM
  2. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 06:26 PM
  3. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 12:28 PM
  4. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  5. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM