【问题标题】:How to iterate through an ArrayList of Objects of ArrayList of Objects?如何遍历 ArrayList of Objects 的对象 ArrayList?
【发布时间】:2014-09-16 14:18:09
【问题描述】:

举个例子:

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

Gun 类的 ArrayList 为 Bullet

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

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

我们可以像这样简单地遍历Gun的ArrayList:

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

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

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

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

【问题讨论】:

  • 为什么你觉得你的too case有区别?只要您的类实现了Iterable(我们知道List 实现了),您就可以使用for-each 循环。
  • 为谁否决了这个问题,愿意在这里放弃理由吗?我已经通过 SO 进行了检查,之前没有人问过这个问题。我希望你能证明你拒绝投票给我的理由。谢谢。
  • 对于任何善良的灵魂,如果您认为我的问题是好的,请给我一个赞成票,以扭转这个不合理的反对票。谢谢。

标签: java object arraylist iteration


【解决方案1】:

您想遵循与以前相同的模式:

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

在这种情况下,它将是:

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

【讨论】:

  • 你给了我我想要的。我试过了,它有效。感谢所有提供答案的人。
【解决方案2】:

编辑:

嗯,他编辑了他的帖子。

如果一个 Object 继承了 Iterable,你可以使用 for-each 循环:

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

所以在你的情况下,如果你想更新你的枪和他们的子弹:

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

首先获取你的第三个 Gun 对象:

Gun g = gunList.get(2);

然后遍历第三把枪的子弹:

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

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

【讨论】:

    【解决方案3】:

    当使用 Java8 时,它会更容易并且只有一个衬里。

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

    【讨论】:

    • 甚至gunList.get(2).getBullets().forEach(System.out::println);
    【解决方案4】:
    for (Bullet bullet : gunList.get(2).getBullet()) System.out.println(bullet);
    

    【讨论】:

    • 认真的吗?我认为一行代码通常不需要解释。尤其是当问这个问题的人自己写道:“for (Gun g: gunList) System.out.println(g);”
    【解决方案5】:

    我们可以做一个嵌套循环来访问列表中元素的所有元素:

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

    【讨论】:

      【解决方案6】:
      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
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-22
        • 2019-04-19
        • 1970-01-01
        • 2018-10-07
        • 1970-01-01
        相关资源
        最近更新 更多