Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the index of the element in the arrayList

Tags:

java

arraylist

I am trying to get the index of 466 in the arrayList minuteList

[288, 318, 346, 376, 406, 436, 466, 1006, 1036, 1066, 1096, 1126, 1156]

but I am getting this error:

java.lang.IndexOutOfBoundsException: Index: 466, Size: 13
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at com.pdf.PDF.refill_time_table(PDF.java:155)
    at com.pdf.PDF.main(PDF.java:54)

I have debugged it and the minuteList has the values above as well the variable element has the value 466. How can I fix it?

I appreciate any help.

Code:

Collections.sort(diffArray);

int element = diffArray.get(diffArray.size() - 1).getElement();
int nextElement = diffArray.get(diffArray.size()-1).getNextElement();
//the error occur after this line.
minuteList.get(element);
like image 973
TheBook Avatar asked Feb 10 '23 22:02

TheBook


1 Answers

minuteList.get(element); gives you the element whose index is element, which doesn't exist in your ArrayList (which only has 13 elements with indices from 0 to 12). Hence the IndexOutOfBoundsException.

You need minuteList.indexOf(element).

like image 106
Eran Avatar answered Feb 23 '23 01:02

Eran