• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

list and array

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

1)created a list
2)printed the list
2)created an array and changed the list to array
4)altered the first element of list
5)change is reflected in the list
6)BUT NOT IN THE ARRAY. THE SAME OLD VALUE IS PRINTED HERE. it should have been changed in the array, when i change the element in the list..???

can anybody explain why is it so??
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it should have been changed in the array, when i change the element in the list..???


No. The array that gets created by toArray is independent of the original List. It contains the same objects, but it will not be updated if the List is updated (the List does not know about the array).

If you look at the source code of ArrayList.toArray you will see that there is no connection between the two.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hari Harann,

Good discovery.

The book (K&B) states that only when you use asList(),any change in one (array or list) also gets updated in the other.

This is however not true for toArray(). This exception is not mentioned in the book though.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic