将boolean []转换为boolean

时间:2018-11-03 15:54:36

标签: java arrays arraylist boolean

所以可以说我有以下代码:

private static boolean[] between nums(double[] numbers, double thisNumber)
    int i = 0;
int length = numbers.length;
boolean [] r = new boolean [length];
for (i = 0; i < length; i++)
{
    double oneWord = values[i];
    if ((oneDouble >= (thisNumber)
    {
        r.equals(true);
    }
}
return r;

r没有返回true。我不知道我在做什么错,但是当我打印出变量r时,它给了我一堆非常奇怪的数字。有什么方法可以将boolean []转换为boolean,还是我在正确的轨道上?

1 个答案:

答案 0 :(得分:1)

如果您要返回一个boolean[],以确保至少可以找到所需的数字。

private static boolean[] atLeast(double[] numbers, double thisNumber)
    boolean[] results = new boolean[number.length];
    for (int i = 0; i < number.length; i++
        results[i] = number[i] >= thisNumber;
    return results;
}

或者,如果您返回List<Boolean>

private static List<Boolean> atLeast(double[] numbers, double thisNumber)
    return DoubleStream.of(numbers)
                       .mapToObj(d -> d >= thisNumber)
                       .collect(Collectors.toList());
}

您似乎正在寻找一种方法,如果值至少等于数字,则该方法返回true。

private static boolean atLeast(double[] numbers, double thisNumber)
    for (double d : numbers)
        if (d >= thisNumber)
            return true;
    return false;
}

使用可以编写的流

private static boolean atLeast(double[] numbers, double thisNumber)
    return DoubleStream.of(numbers)
                       .anyMatch(d >= thisNumber)
}