簡體   English   中英

獲取arrayList中元素的索引

[英]Get the index of the element in the arrayList

我試圖在arrayList minuteList獲得466的索引

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

但我收到這個錯誤:

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)

我調試了它,並且minuteList具有上面的值以及變量element具有值466 我該如何解決?

我感謝任何幫助。

碼:

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

minuteList.get(element); 為您提供索引為element ,該element在您的ArrayList中不存在(其中只有13個索引為0到12的元素)。 因此IndexOutOfBoundsException

你需要minuteList.indexOf(element)

您可以使用indexOf()方法找到元素的索引

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM