This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toArray question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "toArray question" Watch "toArray question" New topic
Author

toArray question

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
I'm confused how the following code works, AND prints out all 7 elements of the original collection? I would of thought there would been an excepton at the very least?

Senthil Kumaran
Ranch Hand

Joined: Aug 24, 2008
Posts: 32
i suggest you to re look the toarray() and aslist() methods again,.,
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Senthil Kumaran wrote: i suggest you to re look the toarray() and aslist() methods again,.,


enlighten me
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

Paul Stat wrote:
Senthil Kumaran wrote: i suggest you to re look the toarray() and aslist() methods again,.,


enlighten me


No offense, but it takes less than a minute to look it up yourself. Why do we need enlighten you?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Henry Wong wrote:
Paul Stat wrote:
Senthil Kumaran wrote: i suggest you to re look the toarray() and aslist() methods again,.,


enlighten me


No offense, but it takes less than a minute to look it up yourself. Why do we need enlighten you?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html

Henry


Ok fine so it


Returns an array containing all of the elements in this list in proper sequence


But what happens to the original String[] array, is it just subject to the garbage collector?
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Paul Stat wrote:
But what happens to the original String[] array, is it just subject to the garbage collector?


If you are talking about this

String[] sa = new String[3]; // intialise sa to accept 3 String objects

Then yes that string array will be eligible for GC. This is the related statement from the documentation

If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: toArray question
 
Similar Threads
Collection.toArray() method
Thread Synchronization??
toArray() method
Printing a List backwards
String [] casting on toArray() necessary for this?