mchan's code tests whether the array is of length 0, which might not be what you're looking for... what use is such an array anyway?
Unfortunately, you don't tell us what you mean by "empty". In case "empty" to you means "has not had a value assigned to an element of the array"...
If your array is of any Object, you might do this if you want to see if any values have been set to the array:
public boolean isEmpty( Object[] arr )
{
for( int i = 0; i < arr.length; ++i )
{
if( arr != null ) return false;
}
return true;
}
Or if, say, your array is of int (or some other primitive type), you could initialize everything and write a less generic method. This is uglier, but I'm trying to be complete ...
// assumes you never need to put this value
// into your array. Nasty, untenable assumption.
public static final int EMPTY = -1;
// initialize the array...
int[] arr = new int[n];
for( int i = 0; i < arr.length; ++i )
{
arr = EMPTY;
}
// your method becomes...
// (good for int arrays only, of course)
public boolean isEmpty( int[] arr )
{
for( int i = 0; i < arr.length; ++i )
{
if( arr != EMPTY ) return false;
}
return true;
}
Or, just wrapper your primitives and use the first method. And maybe use a Collections class (unless you're worried about the overhead) that provides lots of these kinds of services already.
My cat's breath smells like cat food.