[英]For loop logic runs but doesn't make sense
VisitProcedure.length equals 7
VisitProcedure.length 等于 7
So for some reason this method works how I want it to.所以出于某种原因,这种方法可以按照我想要的方式工作。 But the logic makes no sense to me.
但这个逻辑对我来说毫无意义。 I have an array of 7 and i want the user to type in some part of the index 0-6 and it will display the value at that index.
我有一个7的数组,我希望用户输入索引0-6 的某些部分,它将显示该索引处的值。 If they type in a number thats out of bounds then it throws an exception.
如果他们输入的数字超出范围,则会引发异常。 But this is how i read the if statement logic
但这就是我阅读 if 语句逻辑的方式
if index is less then 0 or index is greater then 6 do this (p = VisitProcedure[index].getProcedure(); //displays the index
如果索引小于 0 或索引大于 6 执行此操作 (p = VisitProcedure[index].getProcedure(); //显示索引
But instead it does the opposite.但它恰恰相反。 When i pick 0-6 It displays the value of the array at that index.
当我选择0-6 时,它会在该索引处显示数组的值。 And when i do anything else its out of index.
当我做其他任何事情时,它都没有索引。 Also when i try a different logic
同样,当我尝试不同的逻辑时
if index is greater then equal to 0 and less then 7 do this
如果索引大于等于 0 且小于 7 执行此操作
I still run into error.我仍然遇到错误。 But basically everything works fine it just doesnt make sense to me why.
但基本上一切正常,对我来说为什么没有意义。
public Procedure GetByIndex(int index)throws ArrayIndexOutOfBoundsException {
Procedure p;
if (index < 0 || index > 1 - VisitProcedure.length) { //switching 1 - to - 1 still doesnt work
p = VisitProcedure[index].getProcedure();
return p;
}
else{
ArrayIndexOutOfBoundsException ar;
ar = new ArrayIndexOutOfBoundsException();
throw ar;
//throw new ArrayIndexOutOfBoundsException();
}
}
Your current code is wrong, but it works anyway for the following reasons:您当前的代码是错误的,但由于以下原因它仍然有效:
if you pass a valid index (between 0 and 6), index > 1 - VisitProcedure.length
is true
(since 1 - VisitProcedure.length
is negative), so the if condition is true
and you return VisitProcedure[index].getProcedure()
.如果您传递有效索引(0 到 6 之间),则
index > 1 - VisitProcedure.length
为true
(因为1 - VisitProcedure.length
为负),因此 if 条件为true
并且您返回VisitProcedure[index].getProcedure()
.
If you pass an invalid index > 6
or index < 0
, the condition is still true
, and accessing an invalid index of the array throws ArrayIndexOutOfBoundsException
(ie it is still thrown, but not by the else
clause).如果传递无效
index > 6
或index < 0
,条件仍然为true
,并且访问数组的无效索引会抛出ArrayIndexOutOfBoundsException
(即它仍然会抛出,但不是由else
子句抛出)。
In other words, your condition is always true
, so your code is equivalent to:换句话说,您的条件始终为
true
,因此您的代码等效于:
public Procedure GetByIndex(int index)throws ArrayIndexOutOfBoundsException {
return VisitProcedure[index].getProcedure();
}
If(index>0 && index<=VisitProcedure.length-1){
//if index is greater than 0 and less than or equal to index 6
p = VisitProcedure[index].getProcedure();
return p;
}else{
ArrayIndexOutOfBoundsException ar;
ar = new ArrayIndexOutOfBoundsException();
throw ar;
//throw new ArrayIndexOutOfBoundsException();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.