首页 > 解决方案 > 我的运动冻结了,我不明白为什么

问题描述

我在程序查找重复号码的地方创建了这个问题。因为它不起作用?

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);
            }
        }
    }
}

标签: javaarraysvectornetbeansduplicates

解决方案


您的数组长度为 6。您可以迭代的最大索引为 5。由于您将其从 0 迭代到 6,因此它会为您提供 ArrayIndexOutOfBoundsExcption。您可以通过将循环的条件更改为i<6


推荐阅读