Guava – Shorts.contains()方法及实例
Shorts.contains()是Guava库中Shorts类的一个方法,用于检查目标值是否作为数组中的一个元素存在。目标值和数组都是这个方法的参数。它返回一个布尔值,说明目标值是否存在。
语法:
public static boolean contains(short[] array,
short target)
参数:该方法需要两个强制性参数。
- array:这是一个短值数组,目标将在其中被搜索到。它也可以是空的。
- target: 这是必须在数组中搜索的原始短值。
返回值: 该方法返回一个布尔值,说明目标值是否存在于数组中。如果目标值是存在的,它返回True。
下面的程序说明了上述方法的使用。
例子-1 :
// Java code to show implementation of
// Guava's Shorts.contains() method
import com.google.common.primitives.Shorts;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a short array
short[] arr = { 5, 4, 3, 2, 1 };
short target = 3;
// Using Shorts.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Shorts.contains(arr, target))
System.out.println("Target is present in the array");
else
System.out.println("Target is not present in the array");
}
}
输出:
Target is present in the array
例2 :
// Java code to show implementation of
// Guava's Shorts.contains() method
import com.google.common.primitives.Shorts;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a short array
short[] arr = { 2, 4, 6, 8, 10 };
short target = 7;
// Using Shorts.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Shorts.contains(arr, target))
System.out.println("Target is present in the array");
else
System.out.println("Target is not present in the array");
}
}
输出:
Target is not present in the array
参考资料:
https://google.github.io/guava/releases/23.0/api/docs/com/google/common/primitives/Shorts.html#contains-short:A-short-