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)
1
  • 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) Commented Jul 24, 2013 at 13:12

8 Answers 8

3

Your counter i gets smaller than 0.

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

Sign up to request clarification or add additional context in comments.

2 Comments

OP's is asking for last 5 objects of the list. i >= 0 will not satisfy this requirement.
yep... saw that to late
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--)

1 Comment

dude, i realized the mistake and posted the correct solution. i hope the new one is correct sol . :P
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 .

1 Comment

OP is asking for last 5 objects of the list. your loop goes through all list by inverse order.
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)

Comments

1
Collections.reverse(savedPreventivemachineList);

Then use foreach loop.

Comments

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.

Comments

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);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.