This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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: 18765
    
  40

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
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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://aspose.com/file-tools
 
subject: toArray question