Java 如何遍历对象的 ArrayList 的对象的 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24943663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:53:04  来源:igfitidea点击:

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

javaobjectarraylistiteration

提问by user3437460

Using an example:

使用示例:

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

假设我有一个班级电话Gun。我还有一个班级电话Bullet

Class Gunhas an ArrayList of Bullet.

Gun有一个 ArrayList Bullet

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

要遍历Gun..的 Arraylist 而不是这样做:

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 Gunas such:

我们可以简单地遍历 ArrayListGun如下:

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

Now, I want to iterate and print out all Bulletof my 3rd Gunobject:

现在,我想迭代并打印出Bullet我所有的第三个Gun对象:

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 ?

现在我的问题是:如何使用 ArrayList 迭代打印出枪对象列表,而不是使用传统的 for 循环?

采纳答案by unholysampler

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

回答by Tsotne Tabidze

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

回答by 0-4930-42390eo23o2e0-23oe0-23o

Edit:

编辑:

Well, he edited his post.

好吧,他编辑了他的帖子。

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

如果一个对象继承了 Iterable,你就可以像这样使用 for-each 循环:

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 对象:

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
}

回答by Thorn

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

回答by Andrew Orobator

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
}

回答by Ray A

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

当使用 Java8 时,它会更容易,而且只有一个单行。

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