简体   繁体   English

创建一个函数来检查数组中的任何索引是否具有相同的值

[英]Create a function that checks to see if any indexes in the array are of the same value

I'm trying to create a function which checks to see if any two indexes in the array have the same value. 我正在尝试创建一个函数来检查数组中是否有两个索引具有相同的值。 I've realised my code below only checks to see if the first index is the same as the second, not checking if the first index is the same as the third, fourth and so on. 我已经意识到下面的代码仅检查第一个索引是否与第二个索引相同,而不检查第一个索引是否与第三个,第四个索引相同,依此类推。

I tried using a for loop inside a for loop to compare each index, but I couldn't figure out how to get it to work. 我尝试在for循环中使用for循环比较每个索引,但是我不知道如何使它工作。

Due to my current implementation, I also get an index out of bounds exception due to the i+1 exceeding the length of the array. 由于我当前的实现,由于i + 1超出了数组的长度,因此我也获得了索引超出范围的异常。

If anyone could help solve the code and explain to me how it works that would be great! 如果有人可以帮助解决代码并向我解释它如何工作,那就太好了! Thanks! 谢谢!

public class Values {


public static void main (String[]args){

    int[] A = {0,1,2,1,4};

    for(int i = 0;i<=A.length;i++){

        int n = i+1;
        if(A[i] == A[n]){

        System.out.println("Index " + i + " is the same value as index " + n);
        System.out.println("Therefore, not all of the values in the array are different");
            break;
        }

    }
    System.out.println("All indexes in the array contain different values");
}

}
for(int i = 0; i < A.length - 1; i++){
    for(int j = i + 1; j < A.length; j++){
        if(A[i] == A[j]){
           //do something
        }
    }
}

The most efficient way is to sort first, then find if any two adjacent elements are equal: 最有效的方法是先排序,然后查找两个相邻元素是否相等:

Arrays.sort(A);
for (int i = 0; i < A.length - 1; i++) 
    if (A[i] == A[i+1])
        // do something

This algorithm has O(n log n) time complexity - due to the sort. 由于排序,该算法的时间复杂度为O(n log n)。

Using nested loops has O(n 2 ) time complexity, which will start hurting with even modest array sizes. 使用嵌套循环的时间复杂度为O(n 2 ),即使数组大小适中,也会开始受到损害。 Plus the code is simpler. 再加上代码更简单。

The reason you get an out of bounds exception is because the array is of length 5 (1,2,3,4,5 elements) but only the indices A[0],...,A[4] can be accessed - thus you should loop like this through an array: for(int i = 0;i<A.length;i++) 出现边界异常的原因是因为数组的长度为5(1、2、3、4、5个元素),但是只能访问索引A [0],...,A [4]-因此,您应该像这样遍历数组: for(int i = 0;i<A.length;i++)

public class Values {


    public static void main (String[]args){

        int[] a = {0,1,2,1,4};
        boolean foundMatch = false;

        for(int i = 0;i < a.length; i++){

            if (foundMatch)
                break;

            for (int j = i+1; j < a.length; j++) {


                if (a[i] == a[j]){
                    System.out.println("Index " + i + " & " + j + " contain the same element: " + a[i] + "\nEnding comparison.");
                    foundMatch = true;
                    break;


                    }

            }


        }
        if(!foundMatch)
            System.out.println("All indexes in the array contain different values");
    }

}

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

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