Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why <T> T[] toArray(T[] a) method returns 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 "Why <T> T[] toArray(T[] a) method returns" Watch "Why <T> T[] toArray(T[] a) method returns" New topic
Author

Why <T> T[] toArray(T[] a) method returns

Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Why is this method returning an array containing all of the elements when it can modify the array without returning. Correct me if I'm wrong.

Thanks

_Charles
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I don't really get your question.

Your method uses a generic type as both argument and return value. These must not necessarily have something in common.


JDBCSupport - An easy to use, light-weight JDBC framework -
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Can you please describe your question with more detail ?

The method in question is simply a utility method that you can use to get a typed array.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Even i cannot get what you are trying to ask. But toArray without a destination array in side the () gives out an Object[] array. So to make the list in to a specific type of array we pass the array object type to get the elements from the list in that format.

Well here we are creating an array view of the list. The difference will be any changes in the array won't be reflected in the list and vice versa like we get using asList.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Say we have an ArrayList called iL that stores some Integer objects.
E.g.

Why we need to assign to ia when we are already passing ia to toArray() method

Why is this method returning an array containing all of the elements when it can modify the array without returning.


Thanks

_Charles
Sebastian Kolski
Greenhorn

Joined: Jan 10, 2009
Posts: 1
If passed array is to big or too small new one will be created. So that method need to return that array.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Why is this method returning an array containing all of the elements when it can modify the array without returning


If the list is larger than the array size this method creates a new array to match the size of the list otherwise it returns the same array modified (adding the list elements to it). Check the toArray(T[] a) API.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Sebastian Kolski wrote:If passed array is to big .... new one will be created.


No. If the array is too big one just after the last element will be set to null. Check the API given above.

And Welcome to JavaRanch, Sebastian
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
If passed array is to big or too small new one will be created. So that method need to return that array.


I think new array will only be created when the list is bigger than the array passed.

But the ai on the LHS and RHS in


refers to the same array...isn't that so?

_Charles

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Chikito Charles wrote:But the ai on the LHS and RHS in

refers to the same array...isn't that so?


In this case you are re-assigning the "ia" reference to the returned array object by toArray() which might be the same array object (if the list's size is equal or less than the original array referred by "ia").
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
you are re-assigning the "ia" reference to the returned array object


Point is why reassignment?


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Chikito Charles wrote:Point is why reassignment?

Back to my original question - why the toArray() method returns?


Your question was answered many times in this topic. What is it about the previous answers that you don't understand?

To repeat... The method will create a new array, if the array that is passed doesn't fit all the elments.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Suppose your list contains 7 elements, and the array you pass has a capacity of 5 elements, then a new array will be created and returned. But if the list is of 5 elements and you pass an array with 5 or more capacity, then elements of the list will be placed in the array that you pass and that same array will be returned...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Point is why reassignment?

Back to my original question - why the toArray() method returns?


Sorry for the re-question but why it shouldn't return a value. It has to store the array view somewhere. If it doesn't return the values then where will you store the values? As java is a pass by copy, within the method there will be a different array object which will be out of scope once the method completes. So if you don't return, the entire new array will be lost.
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Java API states...
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection 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 collection.


The API states that a new array with the runtime type and not the array specified at LHS.

In any case, if "ia" (which is used at LHS and RHS) refers to a smaller array than the list, definitely reassignment does not make sense.


Another question is, how do we access the new array created at runtime when the collection doesn't fit?

Thanks

_Charles
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39


In any case, if "ia" (which is used at LHS and RHS) refers to a smaller array than the list, definitely reassignment does not make sense.


In Java, an array is an object, so "ia" is a reference. When assign ia (ie. ia on the LHS), its a reference assignment. It does *not* assign the elements in the array.

So,

ia = iL.toArray(ia);

simply passes a copy of the reference to the object that ia points to the method, and then reassigns the ia reference to the object that is returned. If its the same object, then fine. If not, then ia now references a different object.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Another question is, how do we access the new array created at runtime when the collection doesn't fit?


In your example, via the "ia" reference variable. See previous post.

Henry
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
If its the same object, then fine. If not, then ia now references a different object.


I can imagine where the ia reference will be pointing to and the need for reassignment....and of course why the method returns.

Thanks Henry and thanks to others too.

_Charles

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why <T> T[] toArray(T[] a) method returns
 
Similar Threads
NX: (Contractors) Interface confusion SOS
toArray() Problem?
Problems with returning Arrays
Output of a simple code
Problem with return statement....