Guava的Longs Class的Longs.contains()方法用于检查数组中是否存在值(即目标)。该目标值和数组作为该方法的参数。并且此方法返回一个布尔值,该布尔值说明目标值是否为
用法:
public static boolean contains(long[] array, long target)
参数:此方法接受两个参数:
- array:这是要在其中搜索目标值的值的数组
- target:这是要检查是否存在的long值。
返回值:如果对于i的某个值,在i处存在的值,则该方法返回Trueth索引等于目标,否则返回假。
异常:该方法不会引发任何异常。
范例1:
// Java code to show implementation of
// Guava's Longs.contains() method
import com.google.common.primitives.Longs;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a long array
long[] arr = { 5L, 4L, 3L, 2L, 1L };
long target = 3L;
// Using Longs.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Longs.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 Longs.contains() method
import com.google.common.primitives.Longs;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a long array
long[] arr = { 2L, 4L, 6L, 8L, 10L };
long target = 7L;
// Using Longs.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Longs.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
相关用法
- Java Guava Doubles.min()用法及代码示例
- Java Guava Doubles.contains()用法及代码示例
- Java Guava Floats.contains()用法及代码示例
- Java Guava Bytes.contains()用法及代码示例
- Java Guava Shorts.min()用法及代码示例
- Java Guava Longs.min()用法及代码示例
- Java Guava Chars.min()用法及代码示例
- Java Guava Floats.min()用法及代码示例
- Java Guava Longs.max()用法及代码示例
- Java Guava Doubles.max()用法及代码示例
- Java Guava Chars.max()用法及代码示例
- Java Guava Floats.max()用法及代码示例
- Java Guava Booleans.contains()用法及代码示例
- Java Guava Shorts.contains()用法及代码示例
- Java Guava Chars.contains()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | Longs.contains() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。