• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating an Array from an ArrayList with possible examples  RSS feed

 
kavitha vakiti
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read this paragraph from Mala Gupta Chapter 4. please suggest it with eamples.

"This implies that if you modify the returned array by, say, swapping the position of
its elements or by assigning new objects to its elements, the elements of ArrayList
won’t be affected. But, if you modify the state of (mutable) elements of the returned
array, then the modified state of elements will be reflected in the ArrayList".

Thank you in advance.
 
kavitha vakiti
Greenhorn
Posts: 10
Java
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading the specification of the method: List#toArray(T[] array) (not ArrayList).
What it means is that you might have two references to mutable objects, one in the array and one in the List. You cannot change the array by changing the List and vice versa, but each object is referenced twice so you can see the change in both places.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: the part about the array being “safe” is only to be found in the documentation for the no‑arguments version of toArray.
 
kavitha vakiti
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to get the exact meaning of this statement mentioned in Mala Gupta.
I experienced like if I did changes in Arraylist, I saw the changes in ArrayList not in the Array.
Similarly If I did changes in Array (returned), I saw the changes in Array(returned) not in the ArrayList.

Can Anyone explain the below statement.

"if you modify the state of (mutable) elements of the returned
array, then the modified state of elements will be reflected in the ArrayList".

Thank you.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavitha vakiti wrote: . . .
I experienced like if I did changes in Arraylist, I saw the changes in ArrayList not in the Array.
Similarly If I did changes in Array (returned), I saw the changes in Array(returned) not in the ArrayList.
. . .
The List and the array are now separate. You can change one and the other does not change.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavitha vakiti wrote: . . . "if you modify the state of (mutable) elements of the returned array, . . ." . . .
It is the same object in the array and the same object in the List. If you change its state, both places see the same object so both places see the same changes.

[edit]Add state. Then spell state correctly[/edit]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!