簡體   English   中英

如何在Java中迭代整數arraylist的元素

[英]How to iterate elements of an integer arraylist in Java

我理解當你迭代常規數組元素時,它是這樣的:

int[] counter = new int[10];

for loop{
   counter[i] = 0;
}

when button clicked{
  counter[0]++; //For example
  counter[6]++;
}

但是我不知道如何遍歷arraylist的元素。 如果有人能幫助我理解我會很感激。 謝謝!

最簡單的方法是為每個循環使用a

for(int elem : yourArrayList){
   elem;//do whatever with the element
}

迭代數組列表非常簡單。

您可以使用舊的for loop或使用enhanced for loop

循環好老

int len=arrayList.size();
for(int i = o ; i < len ; i++){
int a =arrayList.get(i);
}

增強了循環

for(int a : arrayList){
//you can use the variable a as you wish.
}
for (int i = 0; i < arrayList.size(); i++) {

}

要么

Iterator<Object> it = arrayList.iterator();
while(it.hasNext())
{
    Object obj = it.next();
    //Do something with obj
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM