Why does Java allow arrays of size 0?

Unlike C, where you can dynamically increase the size of an array, arrays in java are fixed in length. Why does Java allow arrays of size 0, then?

String[] strings = new String[0]; 
+61
java arrays
Jan 6 '11 at 6:40
source share
8 answers

This means that it is empty. That is, you can focus on it, as if it had elements and there was no result:

 for(int k = 0; k < strings.length; k++){ // something } 

Thus, avoiding the need for verification. If the array in question was null , an exception will occur, but in this case it just does nothing, which might be appropriate.

+93
Jan 06 '11 at 6:45
source share

Why does Java allow arrays of size 1? Isn't it useless to wrap one value in an array? Wouldn't it be enough if Java only allowed arrays of size 2 or more?

Yes, we can pass null instead of an empty array and one object or primitive instead of a matrix of size one.

But there are some good arguments against this restriction. My personal main arguments:

The restriction is too complex and not very necessary

To limit the size of arrays to [1..INTEGER.MAX_INT], we would have to add a lot of additional control checks, (agree with Konrads comment), translate the logic and overload method into our code, Exception 0 (and possibly 1) from the allowed sizes array does not save costs, it requires additional effort and adversely affects performance.

Dot matrix vector patterns

An array is a good data model for a vector (mathematics, not the Vector class!). And, of course, the vector in mathematics can be zero. What is conceptually different from nonexistent.




Sidenote - an outstanding wrapper for an array (char -) - is a String class. Immutable String implements the concept of an empty array: it is an empty string ( "" ).

+47
Jan 06 2018-11-11T00:
source share

Sometimes it is much more convenient to return an array of zero size than null.

+20
Jan 06 2018-11-11T00:
source share

Consider this (a more detailed explanation of the noon response):

 public String[] getStrings() { if( foo ) { return null; } else { return new String[] {"bar, "baz"}; } } String[] strings = getStrings(); if (strings != null) { for (String s : strings) { blah(s); } } 

Now compare this to this:

 public String[] getStrings() { if( foo ) { return new String[0]; } else { return new String[] {"bar, "baz"}; } } // the if block is not necessary anymore String[] strings = getStrings(); for (String s : strings) { blah(s); } 

This (returning empty arrays, not null values) is actually the best practice in the Java API development world.

In addition, in Java you can hide lists (e.g. ArrayList) into arrays, and it makes sense to convert an empty list to an empty array.

+11
Jan 06 '11 at 7:11
source share

Same as C ++, it allows you to handle cleaner when there is no data.

+4
Jan 06 '11 at 6:45
source share

Another case where a zero-length array can be useful: To return an array containing all the elements in a list:

 <T> T[ ] toArray(T[ ] a) 

An array of zero length can be used to pass an array type to this method. For example:

 ClassA[ ] result = list.toArray(new ClassA[0]); 

A zero-length array is still an instance of an object that contains zero elements.

+4
May 7 '12 at 13:38
source share

In one case, I can think about where an empty array is extremely useful - use it instead of zero in a situation where null is not allowed. One possible example of this is BlockingQueue arrays. When you want to signal the end of input on the read side, what would you do? Sending a null value seems an obvious choice, but the fact is that BlockingQueue is not null. You can combine your array inside the class with the " boolean last; " field, but this kind of enumeration. Sending an empty (zero size) array seems like the most sensible choice.

+2
Jan 6 2018-11-11T00:
source share

note only:

You say: "Unlike C, where you can dynamically increase the size of the array ...", but in C you cannot change the dimension of the array, the same is for C ++.

+1
Aug 07 '13 at 6:23
source share



All Articles