2014-09-01 132 views
-4

我該如何循環訪問數組,並檢查它是否與Java列表中的項目相等?例如:如何循環訪問Java列表

List<String> arraylist= new ArrayList<String>(); 
arrayList.add("ex3"); 
arrayList.add("ex2"); 
String arrayItems[] = {"ex1", "ex2", "ex3"}; 
int foundItem = 0; 

for (int i = 0; i > arrayItems.length; i++) { 
    if (boughtItems.equals(powerItems[i])) { 
     foundItem++; 
    } 
} 
+3

你的條件是它周圍的錯誤的方式應該是'I'arrayItems.length' – 2014-09-01 20:27:34

+1

都沒有在此代碼4個不同的陣列? – 2014-09-01 20:27:37

回答

0

每個爲這個工作得很好:

for (String element : arraylist) { 
    if (boughtItems.equals(element)) { 
    foundItem++; 
    } 
}