File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab toArray 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 "ExamLab toArray" Watch "ExamLab toArray" New topic

ExamLab toArray

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Henry Wong

Joined: Sep 28, 2004
Posts: 20521

//What is the difference between the following Integer[] {} and new Integer[] { 2, 3, 4 } ??

In the first, an Integer of size zero is instantiated. In the second, an integer of size three, and with 2, 3, and 4, as elements, is instantiated.

In both cases, the passed in array is not used (to hold the vector elements) because it is too small to hold the elements in the vector.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3152

Try to use- new Integer[]{3,5,6,7,8,9,5}

From the Documentation for toArray(): If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null.

Update: So in this case as the length of the array is more than the list- It will accommodate all the elements of the List and the extra size of the array holds null values. So when you try to sort- It gives you an runtime exception.

Mohamed Sanaulla | My Blog
I agree. Here's the link:
subject: ExamLab toArray
It's not a secret anymore!