[英]Jsoup when using .children().get(22) OutOfBoundsException
我想從菜單中選擇42項,當我通過22時,將打印42元素,但它顯示出outofboundsException。 幫幫我。
Elements links3 = doc.select("menu#zz18_ViewSelectorMenu");
for (Element link : links3) {
System.out.println("\nlink : " + link.children().get(22).attr("onMenuClick"));
}
我有這個例外:
FATAL EXCEPTION: Thread-5150
java.lang.IndexOutOfBoundsException: Invalid index 22, size is 22 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at org.jsoup.select.Elements.get(Elements.java:544)
at com.example.projet1.wsActivity$1.run(wsActivity.java:117)
at java.lang.Thread.run(Thread.java:856)
無效的索引22,大小為22
...告訴您ArrayList
的大小為22
,因此您可以使用的最大索引為21
。 List
的索引是從0開始的,因此如果列表中有22個項目,則它們的索引是0
到21
。
因此,為了完整ArrayList
使用大小為5的ArrayList
:
如果需要最后一個元素,請使用get(21)
arraylist的大小為22(arralylist索引從0開始)
for (Element link : links3) {
System.out.println("\nlink : " + link.children().get(21).attr("onMenuClick"));
}
這是捕獲IndexOutofBounds異常的好習慣
它只有22個元素
Elements elements = doc.select("menu#EE18_ViewSelectorMenu");
Elements menuelements =elements.get(0).children();
for (int i = 0; i < menuelements.size(); i++) {
String required = menuelements.get(i)
.attr("onMenuClick");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.