• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExamLab toArray

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic