Questions tagged «indexoutofboundsexception»



15
ArrayList的初始大小
您可以通过执行以下操作来设置ArrayList的初始大小: ArrayList<Integer> arr=new ArrayList<Integer>(10); 但是,你做不到 arr.add(5, 10); 因为它会导致超出范围的异常。 如果您无法访问分配的空间,设置初始大小有什么用? add函数的定义是,add(int index, Object element)所以我不添加索引10。


8
使用ArrayList的迭代器时出现ArrayIndexOutOfBoundsException
现在,我有一个包含以下代码的程序: while (arrayList.iterator().hasNext()) { //value is equal to a String value if( arrayList.iterator().next().equals(value)) { // do something } } 就遍历ArrayList而言,我这样做是否正确? 我得到的错误是: java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.get(Unknown Source) at main1.endElement(main1.java:244) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at …

6
java.lang.IndexOutOfBoundsException:源不适合dest
在以下代码上: static void findSubsets (ArrayList<Integer> numbers, int amount, int index) { ArrayList <Integer> numbersCopy = new ArrayList<Integer>(numbers.size()); Collections.copy(numbersCopy, numbers); } 我收到错误消息: Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest at java.util.Collections.copy(Collections.java:548) at backtracking2.Main.findSubsets(Main.java:61) 为什么?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.