📌  相关文章
📜  Java番石榴 |带有示例的 Booleans.countTrue() 方法

📅  最后修改于: 2022-05-13 01:54:23.212000             🧑  作者: Mango

Java番石榴 |带有示例的 Booleans.countTrue() 方法

Guava 库中Booleans 类countTrue()方法用于统计作为参数传递的指定布尔值中为真值的个数。

句法:

public static int countTrue(boolean... values)

参数:此方法接受布尔值,其中要计算真实值。

返回值:此方法返回一个整数值,它是真值的计数。

示例 1:

// Java code to show implementation of
// Guava's Booleans.countTrue() method
  
import com.google.common.primitives.Booleans;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Boolean array
        boolean[] arr = { false, true, false,
                          true, true };
  
        // Using Booleans.countTrue() method to
        // get the number of values that are true
        System.out.println(Booleans.countTrue(arr));
    }
}
输出:
3

示例 2:

// Java code to show implementation of
// Guava's Booleans.countTrue() method
  
import com.google.common.primitives.Booleans;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a Boolean array
        boolean[] arr = { false, false,
                          false, false };
  
        // Using Booleans.countTrue() method to
        // get the number of values that are true
        System.out.println(Booleans.countTrue(arr));
    }
}
输出:
0

参考: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/primitives/Booleans.html#countTrue-boolean…-