Java番石榴 | Shorts.contains() 方法和示例
Shorts.contains()是 Guava 库中 Shorts 类的一个方法,用于检查值目标是否作为元素存在于数组中的任何位置。目标值和数组都作为该方法的参数。它返回一个布尔值,说明目标值是否存在。
句法:
public static boolean contains(short[] array,
short target)
参数:此方法采用两个强制性参数:
- 数组:这是一个短值数组,其中要搜索的目标。它也可能是空的。
- 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-