在 Java 中检查越界
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19672301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking out of bounds in Java
提问by lemon
I'm trying to check if an array location is out of bounds, what's the simplest way?
我正在尝试检查数组位置是否越界,最简单的方法是什么?
int[] arr;
populate(arr);
if(arr[-1] == null)
//out of bounds!
Would something like this work?
这样的东西会起作用吗?
I'm pretty sure this can be done with a trycatch or a scanner but for just a simple small program, is there another way?
我很确定这可以通过 trycatch 或扫描仪完成,但对于一个简单的小程序,还有其他方法吗?
采纳答案by arshajii
Absolutely do notuse try-catch for this. Simply use:
绝对不要为此使用 try-catch。只需使用:
boolean inBounds = (index >= 0) && (index < array.length);
Implementing the approach with try-catch would entail catching an ArrayIndexOutOfBoundsException
, which is an unchecked exception (i.e. a subclass of RuntimeException
). Such exceptions should never (or, at least, veryrarely) be caught and dealt with. Instead, they should be prevented in the first place.
使用 try-catch 实现该方法将需要捕获ArrayIndexOutOfBoundsException
,这是一个未经检查的异常(即 的子类RuntimeException
)。这些异常应该永远(或至少是非常罕见)被捕获和处理。相反,它们应该首先被阻止。
In other words, unchecked exceptions are exceptions that your program is not expected to recover from. Now, there can be exceptions (no pun intended) to this here and there. For instance, it has become common practice to check if a string is parable as an integer by calling Integer.parseInt()
on it and catching the potential NumberFormatException
(which is unchecked). This is considered OK, but always think twice before doing something like that.
换句话说,未经检查的异常是您的程序预计不会从中恢复的异常。现在,这里和那里可能会有例外(没有双关语)。例如,通过调用一个字符串Integer.parseInt()
并捕捉它的潜力NumberFormatException
(这是未经检查的)来检查一个字符串是否可以比喻为整数已经成为一种常见的做法。这被认为是可以的,但在做这样的事情之前总是要三思而后行。
回答by Paul Draper
No, that will cause an exception.
不,这会导致异常。
Instead, do
相反,做
if (x < 0 || x >= arr.length) {
//x is out of bounds!
}
回答by Little Child
It is better to check if the index is in range manually.
最好手动检查索引是否在范围内。
if(index >=0 && index < array.length){
// in range
}
Exceptions which are unchecked, that is which do not require a try-catch block, should not be caught. ArrayIndexOutOfBoundsException
is one of them.
未经检查的异常,即不需要 try-catch 块的异常,不应被捕获。ArrayIndexOutOfBoundsException
是其中之一。
All unchecked exceptions come from RuntimeException
class.
From this class comes the IndexOutOfBoundsException
This is further specialized into StringIndexOutOfBounds
and ArrayIndexOutOfBounds
所有未经检查的异常都来自RuntimeException
类。
从这个类来IndexOutOfBoundsException
这是进一步专门化StringIndexOutOfBounds
和ArrayIndexOutOfBounds
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
RuntimeException 是那些在 Java 虚拟机正常运行期间可以抛出的异常的超类。
RuntimeException 及其子类是未经检查的异常。未经检查的异常不需要在方法或构造函数的 throws 子句中声明,如果它们可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外。