Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zero length arrays [duplicate]

Tags:

java

Possible Duplicate:
What is the usage of array of zero length?

What is the purpose of zero length arrays.Are they of any use or just like that because the syntax allows?

like image 917
developer Avatar asked Mar 23 '11 13:03

developer


2 Answers

If you have a function that takes an array, and you want to give it an array with nothing in it, you pass an zero-length array.

If you read an array from an external source, and it happens to not have any items, you'll get an zero-length array.

like image 98
SLaks Avatar answered Oct 12 '22 00:10

SLaks


Assuming you mean in Java, you can iterate over zero-length arrays without any problem but you can't do this, if the variable is set to null.

String[] myArr = new String[0];
for (String str : myArr) {
  // do something here
}

If you set myArr to null instead, you'd get a NullPointerException in this loop.

like image 37
Sebastian Krysmanski Avatar answered Oct 12 '22 00:10

Sebastian Krysmanski