Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array length 0

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of creating an array of length 0?
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps a method is supposed to return an array based on some values in a database. Now, suppose that nothing in the database matches what should be contained in the array. Returning an array of length 0 might be a way of communicating to the invoking method that nothing was found.
Of course, there are other ways of accomplishing this task and there are also other uses for arrays of length 0, but this is just one quick example.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey for the quick reply.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting piece of info.
I see now that System.out.println( new int[0].length ); prints 0.
So that's why (in main) you could do:

[ March 25, 2003: Message edited by: Barry Gaunt ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When converting a linked list to an array, the method toArray() creates an array of type Object[].
list.toArray();
Suppose the linked list holds objects of type C. Suppose you want an array of type C[] not Object[].
list.toArray(new C[0]);
You cannot just call list.toArray() and cast the returned array to C[], because toArray created an array of type Object[]. You have to tell toArray to create an array of type C[].

[ March 25, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a big difference between a null array reference and a reference to an empty array´┐Żan empty array is a real object, it simply has no elements. Empty arrays are useful for returning from methods instead of returning null. If a method can return null, then users of the method must explicitly check the return value for null before using it. On the other hand, if the method returns an array which may be empty, no special checking is needed provided the user always uses the array length to check valid indexes. The Java Programming Language, Arnold, Gosling, Holmes, 6.4

- from my favorite Java book, with lots of answers to "why" questions, co-authored by the creator of the Java language
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:

- from my favorite Java book, with lots of answers to "why" questions, co-authored by the creator of the Java language

I agree, Marlene, it's way in front of the crammers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic