java中数组操作常见的三个错误
数组操作常见3个问题
-
当访问但数组中不存在的角标时,ArrayIndexOutOfBoundsException
1 2 3 4 5 6 7 8 | public class ArrayDemo2 { public static void main(String[] args) { int [] array = new int [ 3 ]; //当访问但数组中不存在的角标时 System.out.println(array[ 3 ]); //ArrayIndexOutOfBoundsException } } |
-
当引用型变量没有任何实体指向时,还在用其操作实体, NullPointerException
1 2 3 4 5 6 7 8 | public class ArrayDemo2 { public static void main(String[] args) { int [] array = new int [ 3 ]; //当引用型变量没有任何实体指向时,还在用其操作实体,就会发生该异常 array = null ; System.out.println(array[ 0 ]); //NullPointerException } } |
-
[I@1b6d3586 @左边表示是一个int类型的数组,@右边是内存的hash值;
1 2 3 4 5 6 7 | public class ArrayDemo2 { public static void main(String[] args) { int [] array = new int [ 3 ]; System.out.println(array); //[I@1b6d3586 @左边表示是一个int类型的数组,@右边是内存的hash值; } } |
本文作者:benjieqiang
本文链接:https://www.cnblogs.com/benjieqiang/p/10594583.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步