47

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 ?

3
  • 2
    Why do you think there's a difference between your too case? As long as your class implements Iterable (and we know that List does) you can use a for-each loop.
    – PM 77-1
    Commented Jul 24, 2014 at 20:53
  • For whom down voted this question, care to drop a reason here? I have checked through SO and no one asked this before. I hope you can justify your reason for down voting me. Thanks. Commented Jul 25, 2014 at 9:31
  • 1
    For any kind souls out there, if you think my question is alright, kindly give me an up vote to reverse this unjustified down vote. Thank you. Commented Jul 25, 2014 at 9:41

6 Answers 6

47

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);
}
1
  • You gave me exactly what I asked for. I tried it and it works. Thanks for everyone who contributed an answer. Commented Jul 24, 2014 at 21:02
10

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
}
6

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

    gunList.get(2).getBullets().forEach(n -> System.out.println(n));
1
  • 2
    Or even gunList.get(2).getBullets().forEach(System.out::println);.
    – MC Emperor
    Commented Oct 5, 2017 at 21:16
3
for (Bullet bullet : gunList.get(2).getBullet()) System.out.println(bullet);
1
  • 3
    Seriously? I thought one line of a code usually doesn't need an explanation. Especially when the person who asked the question himself/herself wrote: "for (Gun g: gunList) System.out.println(g);" Commented Jul 24, 2014 at 21:07
2

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(); 
 }
1
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
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.