• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If passed array is to big or too small new one will be created. So that method need to return that array.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you are re-assigning the "ia" reference to the returned array object



Point is why reassignment?


 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic