This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Arrays.asList() 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 "Doubt in Arrays.asList()" Watch "Doubt in Arrays.asList()" New topic
Author

Doubt in Arrays.asList()

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

In my program I have taken two arrays one is String[] array another is int[] array .When I convert these two arrays into List through Arrays.asList().
But list which is created from int[] has the size that is not equal to the array length,but the that is created from String[] has the same size as the length of the String[].Why the size of the list that is created from int[] array is one?



prints
Length of the arr=10
Elements in th list =1
***********************
length of the array =5
Elements in the list =5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20037
    
  44

The asList() method requires an array of objects. An array of ints is *not* an array of objects. However, since the method is written to take var-args, it takes an array of one object -- the int array. Hence, the list of size one.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Doubt in Arrays.asList()
 
It's not a secret anymore!