user3437460
user3437460

Reputation: 17454

How to iterate through an ArrayList of Objects of ArrayList of Objects?

Using an example:

Let say I have a class call Gun. I have another class call Bullet.

Class Gun has an ArrayList of Bullet.

To iterate through the Arraylist of Gun ..instead of doing this:

ArrayList<Gun> gunList = new ArrayList<Gun>();
for (int x=0; x<gunList.size(); x++)
    System.out.println(gunList.get(x));

We can simply iterate through the ArrayList of Gun as such:

for (Gun g: gunList) System.out.println(g); 

Now, I want to iterate and print out all Bullet of my 3rd Gun object:

for (int x=0; x<gunList.get(2).getBullet().size(); x++)  //getBullet is just an accessor method to return the arrayList of Bullet 
    System.out.println(gunList.get(2).getBullet().get(x));

Now my question is: Instead of using the conventional for-loop, how do I printout the list of gun objects using the ArrayList iteration ?

Upvotes: 47

Views: 271893

Answers (6)

Ray A
Ray A

Reputation: 447

When using Java8 it would be more easier and a single liner only.

    gunList.get(2).getBullets().forEach(n -> System.out.println(n));

Upvotes: 6

Edit:

Well, he edited his post.

If an Object inherits Iterable, you are given the ability to use the for-each loop as such:

for(Object object : objectListVar) {
     //code here
}

So in your case, if you wanted to update your Guns and their Bullets:

for(Gun g : guns) {
     //invoke any methods of each gun
     ArrayList<Bullet> bullets = g.getBullets()
     for(Bullet b : bullets) {
          System.out.println("X: " + b.getX() + ", Y: " + b.getY());
          //update, check for collisions, etc
     }
}

First get your third Gun object:

Gun g = gunList.get(2);

Then iterate over the third gun's bullets:

ArrayList<Bullet> bullets = g.getBullets();

for(Bullet b : bullets) {
     //necessary code here
}

Upvotes: 10

Andrew Orobator
Andrew Orobator

Reputation: 8646

int i = 0; // Counter used to determine when you're at the 3rd gun
for (Gun g : gunList) { // For each gun in your list
    System.out.println(g); // Print out the gun
    if (i == 2) { // If you're at the third gun
        ArrayList<Bullet> bullets = g.getBullet(); // Get the list of bullets in the gun
        for (Bullet b : bullets) { // Then print every bullet
            System.out.println(b);
        }
    i++; // Don't forget to increment your counter so you know you're at the next gun
}

Upvotes: 1

Thorn
Thorn

Reputation: 4057

We can do a nested loop to visit all the elements of elements in your list:

 for (Gun g: gunList) {
   System.out.print(g.toString() + "\n   "); 
   for(Bullet b : g.getBullet() {
      System.out.print(g);    
   }
   System.out.println(); 
 }

Upvotes: 2

unholysampler
unholysampler

Reputation: 17321

You want to follow the same pattern as before:

for (Type curInstance: CollectionOf<Type>) {
  // use currInstance
}

In this case it would be:

for (Bullet bullet : gunList.get(2).getBullet()) {
   System.out.println(bullet);
}

Upvotes: 47

Tsotne Tabidze
Tsotne Tabidze

Reputation: 1288

for (Bullet bullet : gunList.get(2).getBullet()) System.out.println(bullet);

Upvotes: 3

Related Questions