Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic