提问者:小点点

检查数组中的每个元素是否具有相同的值


基本上,我想做的是检查int数组中的每个元素,如果所有元素都具有相同的值。

我创建了如下的int数组来传递给比较每个数组元素的方法,它返回布尔true,即使元素并不都是相同的值。

Int[] denominator = {3,3,4,3};

boolean compare;

compare = bruteforce(denominator);

public static boolean bruteforce(int[] input) {

int compare =0;
int count =0;

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

        compare = input[i];

        while(count<input.length){

            if(input[i+1]==compare){
            return true;

            }
            i++;
            count++;

        }//end while

    }//end for
    return false;
}//end method

我想上面的方法将循环并保持数组的每个元素的比较。

当我打印出输出时,它显示它只循环一次,返回布尔值为true。

我真的不知道我的代码中可能出了什么问题。

也许我只是忽略了一些愚蠢的错误。


共3个答案

匿名用户

试试看,

Integer[]    array = {12,12,12,12};
Set<Integer> set   = new HashSet<Integer>(Arrays.asList(array));
System.out.println(set.size()==1?"Contents of Array are Same":"Contents of Array are NOT same");

说明:

将数组添加到集合中,并检查集合的大小,如果是1,则内容相同,否则不相同。

匿名用户

您只需要一个循环,并且应该在适用的情况下尽快返回false(即当您遇到与第一个元素不匹配的元素时)。

您还需要考虑输入数组为null或具有一个元素的边缘情况。

尝试类似这样的东西,这是我根据您提供的代码做的最小改动...

public class BruteForceTest {

    public static boolean bruteforce(int[] input) {

        // NOTE: Cover the edge cases that the input array is null or has one element.
        if (input == null || input.length == 1)
            return true; // NOTE: Returning true for null is debatable, but I leave that to you.

        int compare = input[0]; // NOTE: Compare to the first element of the input array.

        // NOTE: Check from the second element through the end of the input array.
        for (int i = 1; i < input.length; i++) {
            if (input[i] != compare)
                return false;
        }

        return true;

    }

    public static void main(String[] args) {

        int[] denominator = {3,3,4,3};
        boolean compare = bruteforce(denominator);

        // FORNOW: console output to see where the first check landed
        System.out.print("{3,3,4,3}:\t");
        if (compare)
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        // NOTE: a second array to check - that we expect to return true
        int[] denominator2 = {2,2};
        boolean compare2 = bruteforce(denominator2);

        System.out.print("{2,2}:\t\t");
        if (compare2)
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        /*
         *  NOTE: edge cases to account for as noted below
         */

        // array with one element
        int[] denominator3 = {2};
        System.out.print("{2}:\t\t");
        if (bruteforce(denominator3))
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        // null array
        System.out.print("null:\t\t");
        if (bruteforce(null))
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

    }

}

…和输出:

{3,3,4,3}:  Nope!
{2,2}:      Yup!
{2}:        Yup!
null:       Yup!

匿名用户

如果数组元素相等,则只需将第一个元素与其他元素进行比较,因此更好的问题解决方案如下:

public static boolean bruteforce(int[] input) {
     for(int i = 1; i < input.length; i++) {
         if(input[0] != input[i]) return false;
     }

     return true;
}

对于这个简单的算法,您不需要多个循环。希望这有帮助。