• 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

interesting behaviour of asList

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
Noticed an interesting behavior of Arrays.asList(T[]) ,method:


Now i have read in K&B that both this array and list refer to the same data, and changes made to the list will reflect in the array.
But when i try to run this code i get an java.lang.UnsupportedOperationException in line1, as well as line2 (if line1 is commented).

Does anyone know what are the other restrictions on the list?
I found an explanation to this at this blog.
So whats the point of getting an array asList ?
-Meher
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.asList() returns an instance of a collection class whose size cannot be changed. Basically, it's just a thin wrapper over the original array. The intention of this utility method is to just provide a simple convenient bridge between arrays and the Collections API, e.g. when you want to get an Iterator over the array, or use an array as input to another collection's methods.

For more full-fledged manipulation, you'll want to instantiate a new collection and add the array's elements to it, e.g. "List foo = new ArrayList(); foo.addAll(myArray.asList());"
 
Meher Parveen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Kelvin, for explaining that for me.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I knew that the default size of a list while using asList on Array is 11 unless otherwise specified..
Please validate the statement.

Thanks.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandip Sarkar:
I knew that the default size of a list while using asList on Array is 11 unless otherwise specified..
Please validate the statement.



No, this is incorrect. The List returned by Arrays.asList() is always the same size as the underlying array. Besides, how would you "specify otherwise" anyway?
 
reply
    Bookmark Topic Watch Topic
  • New Topic