• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

List.toArray() method

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the K&B book it is written that when we call Arrays.asList() method then the List created as a result of the method call and the Arrays used as the parameter become joined. Any changes made in the array will be reflected in the list and vice versa.

Just now I tried List.toArray() method and if I make changes in array then it is not reflected in the list and other way is also true. I just wanted to know whether this is expected behaviour or I might be doing some thing wrong.

Thanks
Asha
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asha:
Can you post your code..because I tried this asList method and it's running fine...showing each and every change in array as well as list.

Regards
Swapnil
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is always advisable to check the API for Arrays.asList:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm sorry if my question was not very clear. I wanted to ask about the List.toArray() method. When we use this method then the array created is not linked to the list from which it was created as in the case of Arrays.asList() method.

So my question is: The array created from List.toArray method is linked or not to the list from which it was created.

Following is the code that i've written:



Thanks
Asha
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess, the "write-though" only holds for Arrays.asList() and not for Collections.toArray().
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah List.toArray! Don't worry I turn things the other way around all the time (and I misread posts too).

Once again the API comes to the rescue

List.toArray obeys the contract of Collection.toArray (List and Collection are interfaces remember) and Collection.toArray must be implemented such that:

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.


[ June 27, 2006: Message edited by: Barry Gaunt ]
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry. It cleared my doubt.

Asha
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic