java - 我的运动冻结了,我不明白为什么
问题描述
我在程序查找重复号码的地方创建了这个问题。因为它不起作用?
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
推荐阅读
- javascript - 如何防止用户嵌入自定义 HTML
- sql - 创建在两个表上运行的触发器
- python - 按组大小过滤数据框
- python - 单击在帐单地址表单提交会在登录时重定向我,而不是将我重定向到送货地址表单
- sql-server - SQL Server 可以提前查看 XML 标记和/或计算它们吗?
- node.js - 带有批处理请求的 Google Sheets API 更新格式
- python - python - 如何在python中实现具有多个条件的sumifs,包括日期范围?
- python - 优化获取像素到 RGB 和 long if then else list
- python-3.x - 从 Raspberry Pi 到 Parani SD100 的蓝牙连接 - 无可用服务
- reactjs - 从 postgres db 获取数据 - 返回错误:JSON 输入意外结束