java.lang.ArrayIndexOutOfBoundsException(数组越界)处理方法
来源:互联网 发布:淘宝复古连衣裙 编辑:程序博客网 时间:2024/06/10 17:06
当你使用不合法的索引访问数组时会报数组越界这种错误,数组arr的合法错误范围是[0, arr.length-1];当你访问这之外的索引时会报这个错。例如:
public class Test { public static void main(String[] args) { int[] arr = {1, 2, 3}; for (int i = 0; i <= arr.length; i++) { System.out.println(arr[i]); } }}
控制台输出的错误提示:
java.lang.ArrayIndexOutOfBoundsException: 3at Test.main(Test.java:5)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这种错误很像我们下面即将说的字符串索引越界,这种错误的错误信息后面部分与错误不大相关。但是,第1行就告诉我们错误的原因是数组越界了,在我们上面的例子,非法的索引值是3,下面一行的错误信息告诉你错误发生在Test类的第5行上,在main方法之内
public class Test { public static void main(String[] args) { int[] arr = {1, 2, 3}; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }}
当处理数组越界时,打印出遍历数组的索引十分有帮助,这样我们就能够跟踪代码找到为什么索引达到了一个非法的值
阅读全文
0 0
- java.lang.ArrayIndexOutOfBoundsException(数组越界)处理方法
- 数组下标越界异常:Java.lang.ArrayIndexOutOfBoundsException
- System.arraycopy数组越界异常java.lang.ArrayIndexOutOfBoundsException
- Java数组越界错误:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
- [笔记]关于split的数组越界问题以及java.lang.ArrayIndexOutOfBoundsException异常
- 使用Lisview多布局时报数组越界 异常java.lang.ArrayIndexOutOfBoundsException
- 使用Split分割字符串出现数组越界异常java.lang.ArrayIndexOutOfBoundsException
- 关于Java中使用split()方法时报ArrayIndexOutOfBoundsException数组越界异常解决方法
- (数组角标异常)报错java.lang.ArrayIndexOutOfBoundsException..
- 数组下标越界ArrayIndexOutOfBoundsException异常
- java.lang.IndexOutOfBoundsException,数组越界
- Phoenix在建表是报出java.lang.ArrayIndexOutOfBoundsException的处理方法
- ArrayList在多线程调用Add()添加元素时的下标越界问题(java.lang.ArrayIndexOutOfBoundsException)
- ArrayList在多线程调用Add()添加元素时的下标越界问题(java.lang.ArrayIndexOutOfBoundsException)
- 多线程中ArrayList调用Add()添加元素时的下标越界问题(java.lang.ArrayIndexOutOfBoundsException)
- java.lang.ArrayIndexOutOfBoundsException 翻译
- java.lang.ArrayIndexOutOfBoundsException
- java.lang.ArrayIndexOutOfBoundsException
- E
- 如何用intellij idea新建一个java web+Servlet项目
- 7月25日云栖精选夜读:刷脸out了?阿里“刷脚购物”亮相CVPR 2017
- mysql case when 中可以使用and
- Google Instant Apps VS 微信小程序
- java.lang.ArrayIndexOutOfBoundsException(数组越界)处理方法
- 未来10年,最夯的行业大汇总,有你的职位吗?
- Convolutional Neural Network
- 利用Kindeditor、nginx和FTP实现文件上传
- cmd写java(含package)
- Spring Boot 使用 Druid 和监控配置
- 插入排序
- Spark配置
- 如何在一数组中找到相同的值