0

hope someone could help me , I want to display my Last 5 object in my arraylist But , there is something wrong in my code :( , need help please

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

when I execute it shows me this error:

java.lang.IndexOutOfBoundsException: Index: -1, Size: 0
java.util.ArrayList.rangeCheckForAdd(Unknown Source)
java.util.ArrayList.add(Unknown Source)
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Bichoua
  • 7
  • 6
  • for(int i=savedPreventivemachineList.size()-1 ; i >-1 ; i--) maybe? You're going down, not up, so you need to compare until it get 0 position. And your loop is gonna be endless (not really, until outOfBoundsException) – lummycoder Jul 24 '13 at 13:12

8 Answers8

3

Inverse the condition:

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

Set the condition as i>-1 or i>=0. Remember a List list of size: list.size() is indexed from 0 to list.size()-1. The reverse order will be list.size()-1 to 0, as the counter i is gradually decreasing from size()-1,size()-2, .... ,2,1,0.

You can even use a ListIterator for this purpose, that would be safer .

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().

You can read Iterating through a list in reverse order in java to get a hint.

I want to display my Last 5 object in my arraylist

Then the condition should be ((i>savedPreventivemachineList.size()-6) && (i > -1)) . You want the last 5 elements hence savedPreventivemachineList.size()-6 , you need to consider that the index doesn't fall below 0 , hence i>-1 .

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 2
    OP is asking for last 5 objects of the list. your loop goes through all list by inverse order. – erencan Jul 24 '13 at 13:22
3

Your counter i gets smaller than 0.

use (i > savedPreventivemachineList.size() - 5) && (i >= 0) as condition.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
3

To list last 5 objects of a list to your list, loop should run for five times.

For example if size of list is 34

Last accessible index is -> 34 - 1 = 33

Therefore last five indexes are -> 33,32,31,30,29

so size - 6 = 28 meaning that your loop should run until size -6

here is the code.

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

Furthermore, you should check loop index will never go negative values.

To do this, change loop condition by adding i >-1 condition.

for(int i=savedPreventivemachineList.size()-1 ; i > savedPreventivemachineList.size()-6 && i >-1 ; i--)
erencan
  • 3,725
  • 5
  • 32
  • 50
2

Your code as it is now can lead to two scenario's:

  1. If the array size > 5, the condition "i < 5" will fail on the first iteration (because i will be 5 or greater) and your loop will do nothing.
  2. If the array size is 5 or less, "i < 5" will never be false, and the loop will continue forever. This fails because when i becomes less than 0, you are out of bounds of your array.

To solve this: for(int i =savedPreventivemachineList.size()-1 ; i >= 0 && i >= savedPreventivemachineList.size()-5 ; i--)

It will now count back, and step out of the loop if either i becomes -1 (failsafe to prevent out of index errors) or as soon as i is smaller than the size of the array - 5. (reached the 5th item)

Vlemert
  • 316
  • 2
  • 5
1
Collections.reverse(savedPreventivemachineList);

Then use foreach loop.

Maroun
  • 94,125
  • 30
  • 188
  • 241
MayurB
  • 3,609
  • 2
  • 21
  • 36
0

You can use a ListIterator

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

I think this is a little clearer as there isn't a decrementing loop.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

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

Simply int i=savedPreventivemachineList.size()-1 is -1 ie savedPreventivemachineList.size() is Zero

Forward iteration

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

Backward iteration

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

if it is List

List<String> orderList = *****
List<String> reverseList = Lists.reverse(orderList);
internals-in
  • 4,798
  • 2
  • 21
  • 38
0

@erencan, thanks, i have realized the mistake. :)

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

I hope this isn't wrong. :)

PermGenError
  • 45,977
  • 8
  • 87
  • 106