2013-07-24 17 views
0

希望有人可以幫助我,我要展示我的最後5個對象在我的ArrayList但顯示ArrayList中,有在我的代碼:(有些不對勁,需要幫助,請

for(int i=savedPreventivemachineList.size()-1 ; i < 5 ; i--) 
    { 
    Listresult.add(savedPreventivemachineList.indexOf(i), interventionmachine); 
    } 
    savedPreventivemachineTopList=Listresult; 

當我執行它表明我這個錯誤:

java.lang.IndexOutOfBoundsException: Index: -1, Size: 0 
java.util.ArrayList.rangeCheckForAdd(Unknown Source) 
java.util.ArrayList.add(Unknown Source) 
+0

for(int i = savedPreventivemachineList.size() - 1; i> -1; i--)也許?你會下降,而不是上升,所以你需要比較,直到它獲得0的位置。你的循環將會是無止境的(不是真的,直到outOfBoundsException) – lummycoder

回答

0

您可以使用ListIterator

final ListIterator<String> thingIter = things.listIterator(things.size()); 
for (int i = 0; i < 5 && thingIter.hasPrevious(); ++i) { 
    final String thing = thingIter.previous(); 
} 

我認爲這是一個更清晰一點,因爲沒有一個遞減循環。

3

逆條件:

for(int i=savedPreventivemachineList.size()-1 ; i > -1 ; i--) 

設置條件爲i>-1i>=0。請記住List list的大小:list.size()0索引到list.size()-1。相反的順序是list.size()-10,因爲計數器i尺寸()-1,尺寸()-2,...,2,1,0開始逐漸減小。

您甚至可以使用ListIterator爲此目的,這將更安全。

An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

您可以閱讀Iterating through a list in reverse order in java得到一個提示。

I want to display my Last 5 object in my arraylist

然後條件應該是((i>savedPreventivemachineList.size()-6) && (i > -1))。您需要最後的5元素因此savedPreventivemachineList.size()-6,您需要考慮指數不低於0,因此i>-1

+2

OP是要求列表中的最後5個對象。你的循環按逆序排列所有列表。 – erencan

+0

@erencan我現在閱讀,將其包含在我的編輯中。 – NINCOMPOOP

1
Collections.reverse(savedPreventivemachineList); 

然後用foreach循環。

+0

非常感謝,現在它完美的工作:D: – Bichoua

3

你的櫃檯i變得小於0

使用(i > savedPreventivemachineList.size() - 5) && (i >= 0)作爲條件。

+2

OP的要求列表中的最後5個對象。 'i> = 0'不會滿足這個要求。 – erencan

+0

是...看到那個遲到 –

3

要列出列表的最後5個對象到列表中,循環應運行五次。

例如,如果列表的大小是34

最後訪問的索引是 - > 34 - 1 = 33

因此最後五個索引 - > 33,32,31,30,29

如此大小 - 6 = 28意味着您的循環應運行至大小-6

這裏是代碼。

for(int i=savedPreventivemachineList.size()-1 ; i > savedPreventivemachineList.size()-6 ; i--) 
    { 
    Listresult.add(savedPreventivemachineList.indexOf(i), interventionmachine); 
    } 
    savedPreventivemachineTopList=Listresult; 

此外,你應該檢查循環索引永遠不會負值。

爲此,通過添加i >-1條件來更改環路條件。

for(int i=savedPreventivemachineList.size()-1 ; i > savedPreventivemachineList.size()-6 && i >-1 ; i--) 
+0

老兄,我意識到了錯誤併發布了正確的解決方案。我希望新的解決方案是正確的。 :P – PermGenError

0

java.lang.IndexOutOfBoundsException: Index: -1, Size: 0 Shows IndexOutOfBoundsException

簡單int i=savedPreventivemachineList.size()-1是-1 即savedPreventivemachineList.size()Zero

向前迭代

for(Iterator<String> iter = savedPreventivemachineList.iterator(); iter.hasNext();) { 
    String item = iter.next(); 
    //item do what u want 
} 

向後迭代

for(int i=savedPreventivemachineList.size()-1 ; i >= 0 ; i--) 

如果列表

List<String> orderList = ***** 
List<String> reverseList = Lists.reverse(orderList); 
2

您的代碼,因爲它現在是可以導致兩種情景的:

  1. 如果數組的大小> 5,條件「我< 5」將失敗在第一次迭代中(因爲我將是5或更大),你的循環將不會做任何事情。
  2. 如果數組大小爲5或更小,那麼「i < 5」將永遠不會爲假,並且循環將永遠持續。這會失敗,因爲當我變得小於0時,你超出了你的數組範圍。

爲了解決這一: 對(INT I = savedPreventivemachineList.size() - 1; I> = 0 & & I> = savedPreventivemachineList.size() - 5;我 - )

它現在將計數回來,如果我變爲-1(防止失去防止出現索引錯誤),或者只要我小於數組的大小,就退出循環 - 5.(到達第5項)

0

@erencan,謝謝,我已經意識到了這個錯誤。 :)

int size = list.size()-1; 
for(int i=size; i>=(size-5); i--){ 

我希望這是沒有錯的。 :)