aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array length 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array length 0" Watch "array length 0" New topic
Author

array length 0

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
What is the purpose of creating an array of length 0?
Thanks


Sunita<br />SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Corey for the quick reply.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array length 0