empty array?

How can I write a method to check if an array is empty or not?
[76 byte] By [tdkmars] at [2007-9-26 8:31:54]
# 1
public boolean isEmpty() { return 0 == arr.length;}?
mchan0 at 2007-7-1 19:12:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Why should an array be empty?In case there are no elements in an array it may be not useful.
Woti1990 at 2007-7-1 19:12:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

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.

blutofsky at 2007-7-1 19:12:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
$%*!! array-element-access brackets always get stripped out of the code samples. Anyone know how to keep that from happening?Anyay, fill in the open-bracket i close-bracket for the array accesses. They were there when I wrote the post, I swear it!
blutofsky at 2007-7-1 19:12:40 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
> $%*!! array-element-access brackets always get> stripped out of the code samples. Anyone know how to> keep that from happening?Yes; ignore forty years of tradition and use something other than i for array indices.
nerd2004 at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
And put code blocks around your code.
trejkaz at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7
> Yes; ignore forty years of tradition and use something> other than i for array indices.Whaaat!?!? You can't be serious...;)
Kayaman at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8
Ah, so the "i" helps trigger the problem then. Cool. Code blocks, eh? You mean HTML code Blocks? Well, a test... hope it works...<code>arr</code>
blutofsky at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
Oh well. Then what did you mean by "code blocks"? Is there documentation anywhere on how these posts are processed ... I've seen code get mangled on a lot of posts ...
blutofsky at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10
Here they are in the format [ code ] etc.
Kayaman at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11
[ code ] arr [ /code ]
blutofsky at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 12
[ arr ]
blutofsky at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 13
hmmm. I am still missing something, eh?
blutofsky at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 14
No spaces in [ code ] and [ /code ]. The poster had to put spaces in. Otherwise, those would have been interpreted as tags, and you wouldn't have seen them. http://forum.java.sun.com/faq.jsp#messageformat
jverd at 2007-7-1 19:12:41 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 15
Even if you put code blocks around your code your array indexes will show up as <i> if you use i. I just do it anyway and hope people understnd that this happens. When I write a loop I can't help but type i, its automatic.
dubwai at 2007-7-1 19:12:42 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 16
> My cat's breath smells like cat food. My dog's breath smells like poo........
IanMechura at 2007-7-1 19:12:42 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 17
I hope that Sun has fired the person who decided to use [ i ] for the italics tag.
nerd2004 at 2007-7-1 19:12:42 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 18
Yeah, I don't know why they didn't use <i> format.
trejkaz at 2007-7-1 19:12:42 > top of Java-index,Archived Forums,New To Java Technology Archive...