教程 > guava 教程 > 阅读:15

guava booleans 类——迹忆客-ag捕鱼王app官网

返回 guava primitive 实用程序


booleans 是原始类型 boolean 的实用程序类。


类声明

以下是 com.google.common.primitives.booleans 类的声明

@gwtcompatible(emulated = true)
   public final class booleans
      extends object

类方法

序号 方法 说明
1 static list aslist(boolean... backingarray) 返回由指定数组支持的固定大小列表,类似于 arrays.aslist(object[])
2 static int compare(boolean a, boolean b) 以标准方式比较两个指定的布尔值(false 被认为小于 true)。
3 static boolean[] concat(boolean[]... arrays) 将每个提供的数组中的值返回到一个数组中。
4 static boolean contains(boolean[] array, boolean target) 如果 target 作为元素出现在数组中的任何位置,则返回 true。
5 static int counttrue(boolean... values) 返回为真值的数量。
6 static boolean[] ensurecapacity(boolean[] array, int minlength, int padding) 返回包含与数组相同值的数组,但保证具有指定的最小长度。
7 static int hashcode(boolean value) 返回值的哈希码; 等于调用 ((boolean) value).hashcode() 的结果。
8 static int indexof(boolean[] array, boolean target) 返回值 target 在数组中第一次出现的索引。
9 static int indexof(boolean[] array, boolean[] target) 返回指定 target 在数组中第一次出现的起始位置,如果不存在则返回 -1。
10 static string join(string separator, boolean... array) 返回一个字符串,其中包含由分隔符分隔的提供的布尔值。
11 static int lastindexof(boolean[] array, boolean target) 返回值 target 在数组中最后一次出现的索引。
12 static comparator lexicographicalcomparator() 返回一个比较器,该比较器按字典顺序比较两个布尔数组。
13 static boolean[] toarray(collection collection) 将布尔实例的集合复制到原始布尔值的新数组中。

方法继承

该类继承了以下类的方法 -

  • java.lang.object

booleans 类示例

c:/> guava 中使用我们选择的任何编辑器创建以下 java 程序。

guavatester.java

import java.util.list;
import com.google.common.primitives.booleans;
public class guavatester {
   public static void main(string args[]) {
      guavatester tester = new guavatester();
      tester.testbooleans();
   }
   private void testbooleans() {
      boolean[] booleanarray = {true,true,false,true,true,false,false};
      list objectarray = booleans.aslist(booleanarray);
      system.out.println(objectarray.tostring());
      booleanarray = booleans.toarray(objectarray);
      system.out.print("[ ");
      for(int i = 0; i< booleanarray.length ; i  ) {
         system.out.print(booleanarray[i]   " ");
      }
      system.out.println("]");
      system.out.println("true is in list? "   booleans.contains(booleanarray, true));
      system.out.println("true position in list "   booleans.indexof(booleanarray, true));
      system.out.println("true occured: "   booleans.counttrue());
      system.out.println("false vs true: "   booleans.compare(false, true));
      system.out.println("false vs false: "   booleans.compare(false, false));
      system.out.println("true vs false: "   booleans.compare(true, false));
      system.out.println("true vs true: "   booleans.compare(true, true));
   }
}

验证结果

使用 javac 编译器编译类,如下所示

c:\guava>javac guavatester.java

现在运行 guavatester 以查看结果。

c:\guava>java guavatester

结果如下

[true, true, false, true, true, false, false]
[ true true false true true false false ]
true is in list? true
true position in list 0
true occured: 0
false vs true: -1
false vs false: 0
true vs false: 1
true vs true: 0

返回 guava primitive 实用程序

查看笔记

扫码一下
查看教程更方便
网站地图