Given the following code snippet:
int[] arr = {1, 2, 3};
for (int i : arr)
System.out.println(i);
I have the following questions:
I like the answer from 30thh using Iterators from Guava. However, from some frameworks I get null instead of an empty array, and Iterators.forArray(array) does not handle that well. So I came up with this helper method, which you can call with Iterator
public static UnmodifiableIterator emptyIfNull(F[] array) {
if (array != null) {
return Iterators.forArray(array);
}
return new UnmodifiableIterator() {
public boolean hasNext() {
return false;
}
public F next() {
return null;
}
};
}