[英]Loop in arraylist
這是我的ArrayList
:
{10, 20, 30,40, 50, 60, 70, 80, 90, 100....1000}
我想在單擊按鈕時循環我的結果。
我想要第一個Button
的點擊中的前 5 個結果。
然后同樣自動下5個結果。
有什么辦法可以做到嗎?
for(int i=0 ;i<5; i++) {
list = array.getJSONObject(i);
fr.add(list.get("contact"));
}
問題是我只得到前 5 個而不是下一個結果。
謝謝你的幫助。
如果您想在每次從索引 a 到索引 b 調用方法時刪除數組列表中的前 N 個項目,
每次按下該按鈕時ArrayList.removeRange(int fromIndex, int toIndex)
您都可以使用ArrayList.removeRange(int fromIndex, int toIndex)
(但不要重復此操作)
或輕松使用ArrayList.subList(start, end).clear();
為同樣的任務!
從此列表中刪除其索引介於 fromIndex(包含)和 toIndex(不包含)之間的所有元素。 將任何后續元素向左移動(減少它們的索引)。 此調用通過 (toIndex - fromIndex) 元素縮短列表。 (如果 toIndex==fromIndex,則此操作無效。)
閱讀文檔 > http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#removeRange%28int,%20int%29
例子 :
public void removeFistNItemsFromList(){
for(int i=0 ;i<n; i++) {
// add your items or do what ever you want!
}
myArray.subList(0, 4).clear(); // here from 0 to 4th index items will be removed and list will be updated!
System.out.println("MyArrayListNow:"+myArray);
}
每次點擊后輸出:
好吧,你總是試圖閱讀前 5 個元素,因此問題。 您已閱讀並從數組列表中刪除元素的一種選擇。 但是更好的方法是記錄鼠標點擊次數,在這種情況下,這是一個經典的分頁問題,您可以獲取接下來的 5 個元素,並且不會丟失 arraylist 中的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.