Get the index of an element in an array

I am trying to get an index 466in an arrayListminuteList

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

but I get 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 debugged it, and minuteListhas the values ​​above, and the variable elementmatters 466. How can i fix this?

I appreciate any help.

The 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);
+4
source share
2 answers

minuteList.get(element);gives you an element with an index elementthat does not exist in yours ArrayList(which contains only 13 elements with indices from 0 to 12). Consequently, IndexOutOfBoundsException.

You need to minuteList.indexOf(element).

+8
source

You can find the index of an element with the indexOf () method

0
source

All Articles