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.
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.
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.