为什么Java中这段代码的第二行会抛出ArrayIndexOutOfBoundsException
?
String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split(".")[0];
Run Code Online (Sandbox Code Playgroud)
虽然以下工作:
String driveLetter = filename.split("/")[0];
Run Code Online (Sandbox Code Playgroud)
我使用Java 7.
什么ArrayIndexOutOfBoundsException
意思,我该如何摆脱它?
以下是触发异常的代码示例:
String[] name = { "tom", "dick", "harry" };
for (int i = 0; i <= name.length; i++) {
System.out.println(name[i]);
}
Run Code Online (Sandbox Code Playgroud) 为什么列表没有像字典那样安全的"获取"方法?
>>> d = {'a':'b'}
>>> d['a']
'b'
>>> d['c']
KeyError: 'c'
>>> d.get('c', 'fail')
'fail'
>>> l = [1]
>>> l[10]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud) 您可以通过执行设置ArrayList的初始大小
ArrayList<Integer> arr=new ArrayList<Integer>(10);
Run Code Online (Sandbox Code Playgroud)
但是,你做不到
arr.add(5, 10);
Run Code Online (Sandbox Code Playgroud)
因为它会导致越界异常.
如果您无法访问分配的空间,设置初始大小有什么用?
add函数定义为add(int index, Object element)
我没有添加到索引10.
如何n
在不首先进行大小检查(内联是可接受的)或冒险的情况下,IndexOutOfBoundsException
如何使用Java中的字符串的第一个字符?
现在,我有一个包含一段代码的程序,如下所示:
while (arrayList.iterator().hasNext()) {
//value is equal to a String value
if( arrayList.iterator().next().equals(value)) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
我是否正确,只要迭代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 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at main1.traverse(main1.java:73)
at main1.traverse(main1.java:102)
at main1.traverse(main1.java:102)
at main1.main(main1.java:404)
Run Code Online (Sandbox Code Playgroud)
我会显示其余的代码,但它非常广泛,如果我没有正确地进行迭代,我会假设唯一的可能性是我没有ArrayList
正确初始化.
在以下代码中:
static void findSubsets (ArrayList<Integer> numbers, int amount, int index)
{
ArrayList <Integer> numbersCopy = new ArrayList<Integer>(numbers.size());
Collections.copy(numbersCopy, numbers);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
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)
Run Code Online (Sandbox Code Playgroud)
为什么?
我收到的ArrayIndexOutOfBoundsException
时候我用Java 8特性的服务启动(创建bean).
Java 8已经建立并一直在运行.代码编译正确.在服务启动时,服务无法侦听端口,因为bean未创建.当我更改代码(删除java 8构造)时,服务启动,一切正常.
这是我正在使用的代码(服务启动的工作代码):
for (Item itemObject : response) {
if (itemObject.hasId()) {
idList.add(String.valueOf(itemObject.Id());
}
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8构造的相同代码:
response.parallelStream()
.filter(itemObject -> itemObject.hasId())
.map(itemObject -> itemObject.getId())
.forEach(id -> idList.add(id));
Run Code Online (Sandbox Code Playgroud)
包含这段代码的类的bean是使用组件扫描创建的.
当使用第二个代码块代替第一个代码块时,以下是异常消息:
Exiting with throwable: java.lang.IllegalArgumentException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/workspace/.../GetContainerIdForFcSkuAdapter.class]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 51880
java.lang.IllegalArgumentException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/workspace....Some.class]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 51880
Run Code Online (Sandbox Code Playgroud)
对我来说没有意义的是,为什么在创建bean时,函数内部的代码(不是bean类的构造函数)被覆盖.我问这个,因为当我使用普通for循环而不是并行流时,异常不存在.ArrayOutOfBoundsException
当调用函数并且实际使用此代码时,不应该出现.
我该如何解决?
我正在检查JDK 16中IndexOutOfBoundsException的实现,我注意到long
引入了一个带有索引的新构造函数:
/**
* Constructs a new {@code IndexOutOfBoundsException} class with an
* argument indicating the illegal index.
*
* <p>The index is included in this exception's detail message. The
* exact presentation format of the detail message is unspecified.
*
* @param index the illegal index.
* @since 16
*/
public IndexOutOfBoundsException(long index) {
super("Index out of range: " + index);
}
Run Code Online (Sandbox Code Playgroud)
据我所知,数组索引通常是int
值,这在语言规范部分 §10.4 中得到证实:
数组必须按
int
值索引;short
、byte …
根据文档,您可以在List中的任何位置插入对象:
该接口的用户可以精确控制列表中每个元素的插入位置.
(来源:http://download.oracle.com/javase/6/docs/api/java/util/List.html)
但是以下程序因IndexOutOfBoundsException而失败:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<String>();
myList.add(0, "derp");
myList.add(2, "herp");
for (String s : myList) {
System.out.println("Le string: " + s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它也没有明确地设置初始容量(这是有道理的,因为默认值是10).
为什么我不能在任何位置插入对象,只要它的索引低于容量?尺寸是否始终等于插入元素的数量?