• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic