【问题标题】:How to Iterate through two ArrayLists Simultaneously? [duplicate]如何同时遍历两个 ArrayList? [复制]
【发布时间】:2013-04-13 07:09:44
【问题描述】:

我有两个数组列表,声明为:

ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();

这两个字段都包含完全相同的相同数量的值,它们在自然界中实际上是对应的。

我知道我可以像这样遍历其中一个循环:

for(JRadioButton button: category)
{
     if(button.isSelected())
     {
           buttonName = button.getName();
           System.out.println(buttonName);       
     }
}

但是,我想同时遍历这两个 LISTS。我知道它们的尺寸完全相同。我该怎么做?

【问题讨论】:

  • 基本上这应该足够了你为什么要迭代ids?
  • 您应该尝试在这里使用地图而不是两个列表。

标签: java


【解决方案1】:

你可以使用Collection#iterator:

Iterator<JRadioButton> it1 = category.iterator();
Iterator<Integer> it2 = cats_ids.iterator();

while (it1.hasNext() && it2.hasNext()) {
    ...
}

【讨论】:

    【解决方案2】:

    java8 风格:

    private static <T1, T2> void iterateSimultaneously(Iterable<T1> c1, Iterable<T2> c2, BiConsumer<T1, T2> consumer) {
        Iterator<T1> i1 = c1.iterator();
        Iterator<T2> i2 = c2.iterator();
        while (i1.hasNext() && i2.hasNext()) {
            consumer.accept(i1.next(), i2.next());
        }
    }
    //
    iterateSimultaneously(category, cay_id, (JRadioButton b, Integer i) -> {
        // do stuff...
    });
    

    【讨论】:

      【解决方案3】:

      如果您经常这样做,您可以考虑使用辅助函数将两个列表压缩成一对列表:

      public static <A, B> List<Pair<A, B>> zip(List<A> listA, List<B> listB) {
          if (listA.size() != listB.size()) {
              throw new IllegalArgumentException("Lists must have same size");
          }
      
          List<Pair<A, B>> pairList = new LinkedList<>();
      
          for (int index = 0; index < listA.size(); index++) {
              pairList.add(Pair.of(listA.get(index), listB.get(index)));
          }
          return pairList;
      }
      

      您还需要一个 Pair 实现。 Apache commons lang 包有一个合适的。

      有了这些,您现在可以优雅地迭代配对列表:

      ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
      ArrayList<Integer> cat_ids = new ArrayList<Integer>();
      
      for (Pair<JRadioButton, Integer> item : zip(category , cat_ids)) {
         // do something with JRadioButton
         item.getLeft()...
         // do something with Integer
         item.getRight()...
      }
      

      【讨论】:

      • 如果列表的大小不同,这会起作用吗?
      • 没有。这就是存在 throw new IllegalArgumentException... 行的原因。如果两个列表的大小不同,该怎么办并不明显:修剪较长的列表,或使用占位符填充较短的列表。因为占位符可以依赖于列表的类型,所以最好不要在通用实现中处理。
      • 太棒了,太完美了! (对不起thanx评论)
      【解决方案4】:

      试试这个

      ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
      ArrayList<Integer> cat_ids = new ArrayList<Integer>();
      for (int i = 0; i < category.size(); i++) { 
          JRadioButton cat = category.get(i);
          Integer id= cat_ids.get(i);
          ..
      }
      

      【讨论】:

      • 我会添加 i
      • 有点超出范围,但在使用集合时始终使用界面。 List 类别 = new ArrayList();
      • 不是一个好的答案。通常你只知道接口 (List),get(i) 方法使这个解决方案 O(n^2)。避免。
      【解决方案5】:
      ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
      ArrayList<Integer> cat_ids = new ArrayList<Integer>();
      Iterator<JRadioButton> itrJRB = category.iterator();
      Iterator<Integer> itrInteger = cat_ids.iterator();
      while(itrJRB.hasNext() && itrInteger.hasNext()) {
          // put your logic here
      }
      

      【讨论】:

        【解决方案6】:

        虽然您希望两个尺寸相同,但为了安全起见,请获取它们的尺寸并确保它们相等。

        让该大小值为count。然后使用通用 for 循环,迭代直到计数并将值作为数组索引访问。如果 'i' 是索引,则在 for 循环中进行如下访问。

        category[i] and cat_ids[i] 
        

        category[i].isSelected()等等

        【讨论】:

          猜你喜欢
          • 2023-01-10
          • 2015-04-23
          • 2011-02-12
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          • 2023-02-02
          • 2013-03-07
          • 2015-07-11
          相关资源
          最近更新 更多