Java Ints contains() 函数
Guava的 Ints .contains( )函数在数组中的任何地方如果目标元素存在,则返回 true。
语法:
public static boolean
contains(int[] array, int target)
参数: 该方法接受以下参数:
- array: 一个int值的数组,可能为空。
- target: 一个原始的int值。
返回值: 本方法返回一个布尔值。如果数组[i]==目标值,则返回True。
例1:
// Java code to show implementation of
// Guava's Ints.contains() method
import com.google.common.primitives.Ints;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating an Integer array
int[] arr = { 5, 4, 3, 2, 1 };
int target = 3;
// Using Ints.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Ints.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 Ints.contains() method
import com.google.common.primitives.Ints;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating an Integer array
int[] arr = { 2, 4, 6, 8, 10 };
int target = 7;
// Using Ints.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Ints.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/22.0/api/docs/com/google/common/primitives/Ints.html#contains-int:A-int-