我正在基本的 pin 和用户 ID 系统中进行编码,但我似乎无法弄清楚这个索引问题。我知道数组的索引为 0,在我的代码中,我正在执行 while 循环(我尝试了 for 循环,但遇到了同样的问题),用输入的引脚检查数组的第 i 个位置。无论出于何种原因,我收到此错误:
Process: com.example.gabeskillerpcjr.assemblylineapp, PID: 17838 java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at com.example.gabeskillerpcjr.assemblylineapp.FourDtForm.exportVarifaction(FourDtForm.java:350)
at com.example.gabeskillerpcjr.assemblylineapp.FourDtForm$3.onClick(FourDtForm.java:264)
这是我的简单 while 循环,int i 初始化为 0:
public void exportVarifaction(){
String[] PinsNums = new String[]{"1415","1678","1923"};
String[] PinNames = new String[]{"admin","test","test1"};
String tmp;
boolean done = false;
while (!done) {
tmp = PinsNums[i];
if (tmp.equals(keyPadNumsEntered)) {
result = PinNames[i];
loggedOn = true;
done = true;
} else {
++i;
result = "no logon";
}
}
}
最佳答案
在迭代整个数组后,您缺少停止循环的条件。例如:
while (!done) {
tmp = PinsNums[i];
if (tmp.equals(keyPadNumsEntered)) {
result = PinNames[i];
loggedOn = true;
done = true;
} else {
++i;
result = "no logon";
}
if (i == PinsNums.length) {
done = true;
}
}
关于java - 字符串数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51161563/