我有一个String[],其值如下:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
给定字符串s,是否有一种测试VALUES是否包含s的好方法?
我有一个String[],其值如下:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
给定字符串s,是否有一种测试VALUES是否包含s的好方法?
当前回答
检查数组是否包含值的四种不同方法
使用列表:公共静态布尔useList(String[]arr,StringtargetValue){return Arrays.asList(arr).contains(targetValue);}使用集合:公共静态布尔useSet(String[]arr,StringtargetValue){Set<String>Set=newHashSet<String>(Arrays.asList(arr));return set。contains(targetValue);}使用简单循环:公共静态布尔useLoop(String[]arr,StringtargetValue){for(字符串s:arr){如果(s.equals(targetValue))返回true;}return false;}使用Arrays.binarySearch():下面的代码是错误的,为了完整起见,这里列出了它。binarySearch()只能用于已排序的数组。你会发现下面的结果很奇怪。这是对数组进行排序时的最佳选项。public static boolean binarySearch(String[]arr,StringtargetValue){return Arrays.binarySearch(arr,targetValue)>=0;}
快速示例:
String testValue="test";
String newValueNotInList="newValue";
String[] valueArray = { "this", "is", "java" , "test" };
Arrays.asList(valueArray).contains(testValue); // returns true
Arrays.asList(valueArray).contains(newValueNotInList); // returns false
其他回答
使用简单的循环是最有效的方法。
boolean useLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
由Programcreek提供
您可以直接使用Arrays.asList方法以类似的方式将其初始化为List,而不是使用快速数组初始化语法,例如:
public static final List<String> STRINGS = Arrays.asList("firstString", "secondString" ...., "lastString");
然后您可以执行(如上所述):
STRINGS.contains("the string you want to find");
Arrays.asList()->然后调用contains()方法将始终有效,但搜索算法要好得多,因为不需要在数组周围创建轻量级列表包装器,这就是Arrays.as list()所做的。
public boolean findString(String[] strings, String desired){
for (String str : strings){
if (desired.equals(str)) {
return true;
}
}
return false; //if we get here… there is no desired String, return false.
}
开发人员通常会:
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
上面的代码可以工作,但不需要先将列表转换为set。将列表转换为集合需要额外的时间。它可以简单到:
Arrays.asList(arr).contains(targetValue);
or
for (String s : arr) {
if (s.equals(targetValue))
return true;
}
return false;
第一个比第二个更可读。
使用以下方法(在本代码中contains()方法是ArrayUtils.in()):
对象Utils.java
public class ObjectUtils {
/**
* A null safe method to detect if two objects are equal.
* @param object1
* @param object2
* @return true if either both objects are null, or equal, else returns false.
*/
public static boolean equals(Object object1, Object object2) {
return object1 == null ? object2 == null : object1.equals(object2);
}
}
阵列应用程序.java
public class ArrayUtils {
/**
* Find the index of of an object is in given array,
* starting from given inclusive index.
* @param ts Array to be searched in.
* @param t Object to be searched.
* @param start The index from where the search must start.
* @return Index of the given object in the array if it is there, else -1.
*/
public static <T> int indexOf(final T[] ts, final T t, int start) {
for (int i = start; i < ts.length; ++i)
if (ObjectUtils.equals(ts[i], t))
return i;
return -1;
}
/**
* Find the index of of an object is in given array, starting from 0;
* @param ts Array to be searched in.
* @param t Object to be searched.
* @return indexOf(ts, t, 0)
*/
public static <T> int indexOf(final T[] ts, final T t) {
return indexOf(ts, t, 0);
}
/**
* Detect if the given object is in the given array.
* @param ts Array to be searched in.
* @param t Object to be searched.
* @return If indexOf(ts, t) is greater than -1.
*/
public static <T> boolean in(final T[] ts, final T t) {
return indexOf(ts, t) > -1;
}
}
正如您在上面的代码中看到的,还有其他实用方法ObjectUtils.equals()和ArrayUtils.indexOf(),它们也在其他地方使用。