aspose file tools*
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
Author

ExamLab toArray

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

//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.

Henry


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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ExamLab toArray