• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Amazing toArray Method...

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

This is the first time i see this "Magic" code... I could understand how it works, but
I've never seen this kind of logic before...is it common?

The magic i'm referring to, is that when you call "



The compiler takes the array of length Zero, get rid of it, and builds a new one of the correct size
and assigns it to 's'.



Why this? why not simply have a call to a.toArray() and have it done atomatically?
I've would expect an Error thrown at run time saying that the array is not big enough...but
not just a silent replacement...

The API documentation says:


the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.



In addition to the above, I feel this could be quite dangerous, as shown in the following example:

The code below, creates an Array of size '5'. Then two values are added, one at position [0] and
one at position [4]. When we run the 'a.toArray(s)' method, fields are overwritten and not added...
I wonder why there is a mix. I would have expected Either "Completely Replaced" or Either "Added",
but not a mix of the two. This leaves the array in an inconsistent state...



Thank you for any clarification on this!
Dave
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Davide Crudo wrote:why not simply have a call to a.toArray() and have it done atomatically?


Because a.toArray() returns an Object[], not a String[]. If you try to cast it to String[] you will get a ClassCastException.

I've would expect an Error thrown at run time saying that the array is not big enough...but not just a silent replacement...


As the API said, the toArray method will take the array argument, and if it is too small it will basically create an exact copy as far as its type is concerned. For instance, an Integer[] will lead to an Integer[], a String[] to a String[], etc.

In addition to the above, I feel this could be quite dangerous, as shown in the following example:

The code below, creates an Array of size '5'. Then two values are added, one at position [0] and
one at position [4]. When we run the 'a.toArray(s)' method, fields are overwritten and not added...
I wonder why there is a mix. I would have expected Either "Completely Replaced" or Either "Added",
but not a mix of the two. This leaves the array in an inconsistent state...


That is dangerous yes, but you can always still use the collection's size().

As for your "magic" call, the following is just a bit better:
It will not create a new array inside the method; this is basically the same as a.toArray() but with the proper array type.
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic