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);
}
}
arrayutils.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 ;
}
}
ArrayList<Integer> arrlist = new ArrayList<Integer>(8);
// use add() method to add elements in the list
arrlist.add(20);
arrlist.add(25);
arrlist.add(10);
arrlist.add(15);
boolean retval = arrlist.contains(10);
if (retval == true) {
System.out.println("10 is contained in the list");
}
else {
System.out.println("10 is not contained in the list");
}
public static <T> boolean contains(final T[] array, final T v) {
for (final T e : array)
if (e == v || v != null && v.equals(e))
return true;
return false;
}
改进: 这个 v != null 方法内部的条件是恒定的。在方法调用期间,它总是计算为相同的布尔值。所以如果输入 array 是大的,只计算一次这个条件更有效,我们可以在内部使用一个简化的/更快的条件 for 基于结果的循环。改进的 contains() 方法:
public static <T> boolean contains2(final T[] array, final T v) {
if (v == null) {
for (final T e : array)
if (e == null)
return true;
}
else {
for (final T e : array)
if (e == v || v.equals(e))
return true;
}
return false;
}
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.
}
29条答案
按热度按时间3qpi33ja1#
使用
Array.BinarySearch(array,obj)
是否在数组中查找给定对象。例子:
false—不存在
uhry853o2#
如果你不想区分大小写
iqih9akk3#
使用以下方法
contains()
方法是ArrayUtils.in()
在本规范中):objectutils.java文件
arrayutils.java文件
正如您在上面的代码中看到的,还有其他实用程序方法
ObjectUtils.equals()
以及ArrayUtils.indexOf()
,在其他地方也有使用。qzwqbdag4#
你可以用两种方法来检查
a) 通过将数组转换为字符串,然后通过.contains方法检查所需的字符串
b) 这是一种更有效的方法
nx7onnlm5#
试试这个:
1u4esq0p6#
由于我正在使用基本类型byte和byte[]处理低级java,所以到目前为止,我得到的最好的结果是bytes-javahttps://github.com/patrickfav/bytes-java 似乎是个好作品
slhcrj9b7#
使用
Spliterator
防止不必要的List
```boolean found = false; // class variable
String search = "AB";
Spliterator spl = Arrays.spliterator( VALUES, 0, VALUES.length );
while( (! found) && spl.tryAdvance(o -> found = o.equals( search )) );
```
found == true
如果search
包含在数组中这适用于基本体数组
jvlzgdj98#
看看这个
dsf9zpds9#
在Java8中使用流。
brvekthn10#
尝试使用Java8 predicate 测试方法
下面是一个完整的例子。
http://mytechnologythought.blogspot.com/2019/10/java-8-predicate-test-method-example.html
https://github.com/vipulgulhane1/java8/blob/master/test.java
kzmpq1sx11#
如果数组没有排序,您将不得不遍历所有内容并在每个数组上调用equals。
如果数组被排序,你可以做一个二进制搜索,在arrays类中有一个。
一般来说,如果要进行大量的成员资格检查,可能需要将所有内容存储在一个集合中,而不是存储在一个数组中。
0dxa2lsx12#
只需手动执行即可:
改进:
这个
v != null
方法内部的条件是恒定的。在方法调用期间,它总是计算为相同的布尔值。所以如果输入array
是大的,只计算一次这个条件更有效,我们可以在内部使用一个简化的/更快的条件for
基于结果的循环。改进的contains()
方法:wnavrhmk13#
开发人员通常会:
上面的代码可以工作,但是不需要先将列表转换为set。将列表转换为集合需要额外的时间。它可以简单到:
或
第一个比第二个可读。
oymdgrw714#
arrays.aslist()->然后调用contains()方法将始终有效,但是搜索算法要好得多,因为不需要在数组周围创建轻量级的列表 Package 器,而arrays.aslist()就是这样做的。
gpfsuwkq15#
创建一个最初设置为false的布尔值。运行一个循环来检查数组中的每个值,并与要检查的值进行比较。如果得到匹配,请将boolean设置为true并停止循环。然后Assert布尔值为真。