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

用户名

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

我如下创建int数组,以传递给比较每个数组元素的方法,即使元素不是全部相同的值,它也会返回boolean 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。

我真的失去了线索,可能是我的代码出了什么问题。

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

J0e3gan

您只需要一个循环,并应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!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查数组的所有元素在Swift中是否具有相同的值

检查数组中的每个元素是否都有特定值

检查数组是否具有相同的值

检查数组是否具有相同的值和相同的频率

检查另一个数组中的数组是否具有相同的元素

如何检查数组的3个元素是否具有相同的值

Javascript:具有相同值的对象数组的每个元素

数组中数组的检查值具有相同的值

如何检查前一个标签是否与数组中的某些值具有相同的值

jQuery-检查数组中的2个元素是否具有相同的类

如何检查对象数组是否具有相同的值

检查任何行是否具有与 numpy 数组相同的值

如何在 Julia 中测试数组中的所有元素是否具有相同的值

Hamcrest 匹配器用于检查响应 json 数组中的任何元素是否具有与 Rest Assured 中的特定值相同的属性值

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

检查数组中的多个元素是否包含相同的值

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

如何检查对象的Javascript数组是否在整个过程中具有相同的值

检查元素数组中的元素是否具有焦点

如何检查节点列表中的所有元素是否具有相同的类或相同的样式属性值?

检查数组的任何元素是否在 bash 中具有特定值的条件

从元组数组中删除在每个元素的第一个索引位置具有相同值的元素

检查数组是否具有相同的条目

对对象数组中具有相同值的元素求和

从数组中删除具有相同属性值的元素

检查列表中的元素是否在定义的位置具有值

有没有办法检查两个数组是否具有相同的元素?

验证值是否在具有相同键名的Json数组中

如何检查两个数组在每个位置是否具有相同的参数,我