[英]my exercise freezes and I don't understand why
我在程序查找重复号码的地方创建了这个问题。 因为它不起作用?
package javaapplication5;
/**
*
* @author miste
*/
public class JavaApplication5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
NewClass n = new NewClass();
int[] numero = {2, 3, 4, 5, 6, 3};
n.cerca(numero);
}
}
package javaapplication5;
import static java.lang.reflect.Array.get;
/**
*
* @author miste
*/
public class NewClass {
int [] numero = {2, 3, 4, 5, 6, 3};
public void cerca(int[] numero) {
int tmp = 0;
for (int i = 0; i < 7; i++) {
tmp = numero[i];
}
for (int j = 0; j < 7; j++) {
if (numero[j] == tmp) {
System.out.println("il duplicato è " + tmp);
}
}
}
}
您的数组长度为 6。您可以迭代到的最大索引为 5。由于您从 0 到 6 对其进行迭代,因此它为您提供了一个 ArrayIndexOutOfBoundsExcption。 您可以通过将循环条件更改为i<6
来纠正它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.