• 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

List converted from an Array

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

Why the following code giving UnsupportedOperationException at Line1 when running the program? Can't we able to remove a value from list, which is converted from an array? please explain?


Thanks in Advance.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,






asList() method of ArrayList returns fixed sized ArrayList.
And this ArrayList is not the List that implements List interface,
but sub class of Arrays class.

Try this:



You will get the output as:
java.util.Arrays$ArrayList

and prove your answer with this code:


Arrays.ArrayList extends AbstractList class that implement List
interface.



What operation is supported in that are:
get() and set()
remove() and add() throws UnsupportedOperationException, because these
operations are not supported by it.

And yeah, one more thing; if you use the set() method on the list it
will reflect the array also and vice versa;
Check it out!!!





Regards,
cmbhatt
[ April 23, 2007: Message edited by: Chandra Bhatt ]
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

Thanks for Explanation. I got it.

Thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic