开发中常遇到到的问题二:java.lang.ArrayIndexOutOfBoundsException

开发常见错误ArrayIndexOutOfBoundsException(越界)
这个一般都是在取值或者是赋值的时候最为常见:

public class ExampleUnitTest {  
    private String[] data;  
    @Test  
    public void addition_isCorrect() throws Exception {  
        data = new String[]{}; //初始化  
        data[0] = "李四"; //这里就会ArrayIndexOutOfBoundsException  
    }  
}  

错误:

java.lang.ArrayIndexOutOfBoundsException: 0  
    at com.example.mytest.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.java:29)  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:498)  
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)  
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)  
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)  
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)  
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)  
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)  
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)  
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)  
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)  
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)  
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)  
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)  
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)  
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)  
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)  
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)  
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:498)  
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)  

看错误能知道data这个数组是没有给分配空间导致下标0报错,那么在初始化地方修改下代码

public class ExampleUnitTest {  
    private String[] data;  
    @Test  
    public void addition_isCorrect() throws Exception {  
        data = new String[1]; //初始化  
        data[0] = "李四"; //这里就会ArrayIndexOutOfBoundsException  
    }  
}  

这样在运行代码就OK了;
取值也一样,比方现在data数组有下标0了,那么取下标1的数据data[1]可以吗,我们试试。

public class ExampleUnitTest {  
    private String[] data;  
    @Test  
    public void addition_isCorrect() throws Exception {  
        data = new String[1]; //初始化  
        data[0] = "李四"; //这里就会ArrayIndexOutOfBoundsException  
        System.out.print(data[1]);  
    }  
}  

结果:

java.lang.ArrayIndexOutOfBoundsException: 1  
    at com.example.mytest.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.java:30)  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:498)  
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)  
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)  
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)  
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)  
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)  
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)  
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)  
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)  
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)  
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)  
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)  
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)  
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)  
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)  
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)  
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)  
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    at java.lang.reflect.Method.invoke(Method.java:498)  
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)  

果断的报错了,那是因为数组一个就只有一位长度,现在在去值第二位当然就报错了;(下标都是已0开始的),我们把

System.out.print(data[1]);  

改为

System.out.print(data[0]);  

运行就可以得李四的值,也就是data数组保存的值。
结束语:
但你在取某个集合或数组的长度大的下标数据时就会报错,但保存没有定义数组长度的时候也会报错,这是我的一个理解。

你可能感兴趣的:(开发中常遇到到的问题二:java.lang.ArrayIndexOutOfBoundsException)