简体   繁体   English

我的运动冻结了,我不明白为什么

[英]my exercise freezes and I don't understand why

I created this problem where the program looks for the duplicate number.我在程序查找重复号码的地方创建了这个问题。 because it does not work?因为它不起作用?

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

Your array is of length 6. The maximum index you can iterate to is 5. Since you are iterating it from 0 to 6, it gives you an ArrayIndexOutOfBoundsExcption.您的数组长度为 6。您可以迭代到的最大索引为 5。由于您从 0 到 6 对其进行迭代,因此它为您提供了一个 ArrayIndexOutOfBoundsExcption。 You can correct it by changing the condition of the loop to i<6您可以通过将循环条件更改为i<6来纠正它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM