I was looking through the code for an old Android application of mine, and I saw one thing I did to the effect of this:
boolean emptyArray = true;
for (int i = 0; i < array.size(); i++)
{
if (array.get(i) != null)
{
emptyArray = false;
break;
}
}
if (emptyArray == true)
{
return true;
}
return false;
There has to be a more efficient way of doing this — but what is it?
emptyArray is defined as an ArrayList of Integers, which are inserted with a random number of null values (And later in the code, actual integer values).
Thanks!
There is no more efficient way.
The only thing is you can do, is write it in more elegant way:
Actually, it is possible that this is more efficient, since it uses
Iteratorand the Hotspot compiler has more info to optimize instead usingsize()andget().