1.效率最高(最原始)
代码如下(示例):
public class Demo {
public static boolean useLoop(String[] arr, String targetValue) {
for (String s : arr) {
if (s.equals(targetValue)) return true;
}
return false;
}
public static void main(String[] args) {
String arr[] = {"aa", "bb", "cc"};
String targetValue = "bb";
System.out.println(useLoop(arr, targetValue));
}
}
运行结果:

2.List数组Contains
代码如下(示例):
import java.util.Arrays;
public class Demo {
public static boolean useList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static void main(String[] args) {
String arr[] = {"aa", "bb", "cc"};
String targetValue = "bb";
System.out.println(useList(arr, targetValue));
}
}
运行结果:

3.Set的Contains
代码如下(示例):
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Demo {
public static boolean useSet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static void main(String[] args) {
String arr[] = {"aa", "bb", "cc"};
String targetValue = "bb";
System.out.println(useSet(arr, targetValue));
}
}
运行结果:

4.Arrays的binarySearch
代码如下(示例):
import java.util.Arrays;
public class Demo {
public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if (a > 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String arr[] = {"aa", "bb", "cc"};
String targetValue = "bb";
System.out.println(useArraysBinarySearch(arr, targetValue));
}
}
运行结果:








![[QT编程系列-4]:C++图形用户界面编程,QT框架快速入门培训 - 2- QT程序的运行框架:信号、槽函数、对象之间的通信](https://img-blog.csdnimg.cn/593120b7676a468d9a242ba0fd4f4584.png)










