我有两个arraylist但是如何在一个迭代器循环中显示这两个值?感谢他们的帮助。
ArrayList alRateCode = new ArrayList();
ArrayList alRateCode2 = new ArrayList();
ArrayList alInner = new ArrayList();
ArrayList alInner2 = new ArrayList();
alInner.add("A");
alInner.add("Apple");
alInner.add("1KG");
alInner.add("24.00");
alRateCode.add(alInner);
alInner2.add("B");
alInner2.add("Banana");
alInner2.add("1KG");
alInner2.add("10.00");
alRateCode2.add(alInner2);
for(Iterator i = alRateCode.iterator(); i.hasNext();)
{
ArrayList alInner = (ArrayList) i.next();
String Code = (String) alInner.get(0); // apple details
String DESCP= (String) alInner.get(1);
// how to loop banana details in this iterator
}
答案 0 :(得分:1)
Iterator i = alRateCode.iterator();
Iterator i2 = alRateCode2.iterator()
while(i.hasNext() && i2.hasNext()) {
ArrayList appleInner = (ArrayList) i.next();
ArrayList bananaInner = (ArrayList) i2.next();
//Other code
}
答案 1 :(得分:0)
你的问题答案是:
在for循环中使用初始索引初始化某个变量并从alRateCode2
for(int j=0,Iterator i = alRateCode.iterator(); i.hasNext();j++)
{
ArrayList alInner = (ArrayList) i.next();
String Code = (String) alInner.get(0); // apple details
String DESCP= (String) alInner.get(1);
// how to loop banana details in this iterator
ArrayList blInner = (ArrayList) alRateCode2.get(j);
String code1 = (String) blInner.get(0); // banana details
String DESCP2= (String) blInner.get(1);
}
答案 2 :(得分:0)
你可以创建一个方法,你可以将1-n arraylists作为参数传递并循环遍历它:
public static <T> void loopThroughArrayLists(ArrayList<ArrayList<T>>... lists) {
for (ArrayList<ArrayList<T>> list : lists) {
for (ArrayList<T> elements : list) {
String Code = (String)elements.get(0);
String DESCP = (String)elements.get(1);
}
}
}
//usage
loopThroughArrayLists(alRateCode, alRateCode2);
但在我看来,你应该为你的&#34;更低的&#34; arraylist(&#34; A&#34;,&#34; Apple&#34;,&#34; 1KG&#34;,&#34; 24.00&#34;)。这听起来应该像#34; FruitPrice&#34;或类似的东西。