Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何同时迭代两个ArrayList?_Java - Fatal编程技术网

Java 如何同时迭代两个ArrayList?

Java 如何同时迭代两个ArrayList?,java,Java,我有两个数组列表,声明为: ArrayList<JRadioButton> category = new ArrayList<JRadioButton>(); ArrayList<Integer> cat_ids = new ArrayList<Integer>(); 但是,我想同时迭代这两个列表。我知道它们的尺寸完全一样。我该怎么做?试试这个 ArrayList<JRadioButton> category = new ArrayL

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

ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();
但是,我想同时迭代这两个列表。我知道它们的尺寸完全一样。我该怎么做?

试试这个

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);
    ..
}
ArrayList类别=新建ArrayList();
ArrayList目录ID=新的ArrayList();
对于(inti=0;i
您可以使用:

Iterator it1=category.Iterator();
迭代器it2=cats_id.Iterator();
while(it1.hasNext()&&it2.hasNext()){
...
}

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

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

category[i] and cat_ids[i] 
category[i].isSelected()
等等

ArrayList category=new ArrayList();
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
}
ArrayList目录ID=新的ArrayList(); 迭代器itrJRB=category.Iterator(); 迭代器itriger=cat_id.Iterator(); while(itrJRB.hasNext()&&itrInteger.hasNext()){ //把你的逻辑放在这里 }
< /代码> 如果您经常这样做,您可以考虑使用助手函数将两个列表压缩为一对列表:

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;
}
publicstaticlist-zip(List-listA,List-listB){
如果(listA.size()!=listB.size()){
抛出新的IllegalArgumentException(“列表必须具有相同的大小”);
}
List pairList=newlinkedlist();
对于(int index=0;index
您还需要一个Pair实现。ApacheCommonsLang包有一个合适的包

有了这些,您现在可以优雅地在pairlist上迭代:

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()...
}
ArrayList类别=新建ArrayList();
ArrayList目录ID=新的ArrayList();
用于(配对项目:zip(类别,类别ID)){
//用JRadioButton做点什么
item.getLeft()。。。
//用整数做些什么
item.getRight()。。。
}
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...
});
private static void同时迭代(Iterable c1、Iterable c2、双消费者){
迭代器i1=c1.Iterator();
迭代器i2=c2.Iterator();
while(i1.hasNext()&&i2.hasNext()){
accept(i1.next(),i2.next());
}
}
//
同时迭代(类别,cay_id,(JRadioButton b,整数i)->{
//做些事情。。。
});

我会添加IArrayList<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()... }
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...
});