This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 18141
    
  39

//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: 3056
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExamLab toArray
 
Similar Threads
Sorting Arraylist of objects base on their integers feild
Shutdown hook
implementing and extending
Number of String objects
Comparable..