如何同时遍历两个ArrayList? [英] How to Iterate through two ArrayLists Simultaneously?

查看:230
本文介绍了如何同时遍历两个ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Have Two Array Lists, Declared as:

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

这两个字段都包含完全相同的值的相同"值,实际上在自然界中是对应的.

Both of the these fields contain exactly, the Same No of Values, which are infact corresponding in Nature.

我知道我可以遍历这样的循环之一:

I know I can iterate over one of the loops like this:

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

但是,我想同时遍历两个列表.我知道它们的尺寸完全相同.我该怎么办?

But, I would like to iterate over both the LISTS simultaneously. I know they have the exact same size. How do I Do that?

推荐答案

您可以使用

You can use Collection#iterator:

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

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

这篇关于如何同时遍历两个ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆