fre*_*low 6 c++ java arrays algorithm null
我想检测数组的子范围是否包含空引用.不知何故这样:
public static <T> boolean containsNull
(T[] array, int fromInclusive, int toExclusive)
{
for (int i = fromInclusive; i < toExclusive; ++i)
{
if (array[i] == null) return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
在Java库中是否有这样的方法,所以我不必手动遍历数组?也许我被C++对支持算法的代码的出色支持所破坏,我可以写下:
#include <algorithm>
bool found_null = (std::find(array + from, array + to, 0) != array + to);
Run Code Online (Sandbox Code Playgroud)
SLa*_*aks 22
检查是否Arrays.asList(myArray).contains(null).
要检查数组的一部分,请检查是否
Arrays.asList(myArray).subList(from, to).contains(null)
Run Code Online (Sandbox Code Playgroud)
这不会创建不必要的数组副本; 双方asList并subList创建ArrayList和RandomAccessSubList那个包裹原始数组不复制它的对象.
Apache commons-lang为您提供ArrayUtils.contains(array, null)
对于范围:ArrayUtils.contains(Arrays.copyOfRange(array, from, to), null)
| 归档时间: |
|
| 查看次数: |
9702 次 |
| 最近记录: |