java – 在这种情况下如何避免ArrayIndexOutOfBoundsException?
发布时间:2020-12-15 04:12:07 所属栏目:Java 来源:网络整理
导读:参见英文答案 What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?????????????????????????????????????23个 在这种情况下是否可以避免ArrayIndexOutOfBoundsException? package com;public class Hi { public static void
参见英文答案 >
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?????????????????????????????????????23个
在这种情况下是否可以避免ArrayIndexOutOfBoundsException? package com; public class Hi { public static void main(String args[]) { String[] myFirstStringArray = new String[] { "String 1","String 2","String 3" }; if (myFirstStringArray[3] != null) { System.out.println("Present"); } else { System.out.println("Not Present"); } } } 解决方法
也许我不明白真正的问题,但在这种情况下,什么阻止你在访问它之前检查索引是否在数组内?
if (myIndex < myFirstStringArray.length) { System.out.println("Present"); } else { System.out.println("Not Present"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |