如果目標在數組中的任何位置作為元素存在,則 Guava 的Ints.contains()返回true。
用法:
public static boolean contains(int[] array, int target)
參數:此方法接受以下參數:
- array:一個int值數組,可能為空。
- target:基本的int值。
返回值:此方法返回一個布爾值。如果array [i] ==目標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
相關用法
- Java Guava Ints max()用法及代碼示例
- Java Guava Ints min()用法及代碼示例
- Java Guava Ints lastIndexOf()用法及代碼示例
- Java Guava Ints indexOf()用法及代碼示例
- Java Guava Ints join()用法及代碼示例
- Java Guava Ints toArray()用法及代碼示例
- Java Guava Ints concat()用法及代碼示例
- Java Guava Ints asList()用法及代碼示例
- Java Guava Ints.hashCode()用法及代碼示例
- Java Guava Ints.compare()用法及代碼示例
- Java Ints.indexOf(int[] array, int[] target)用法及代碼示例
- Java Guava Sets union()用法及代碼示例
- Java Guava Sets difference()用法及代碼示例
- Java Guava BigIntegerMath binomial()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Ints contains() function | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。