• 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

question about Iterator remove method

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

I want to remove several items from a collection using Iterator's remove method,
but I got UnsupportedOperationException. Is there a way to solve thos problem?

thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of collection are you using here? How did you obtain it? Whether or not remove() is supported is determined by how & where you got the collection - usually, if you consult the docs for the method or constructor used to create or access the collection, it will tell you if remove() is supported (as well as other methods). As an example, for a newly-created ArrayList or LinkedList, remove is supported by default. But if you wrap the list using Collections.unmodifiableList(), or if you have an array and you convert it to a list with Arrays.asList(), then remove() is not supported. So, what alternatives do you have? It's hard to say - we'd really need to know more about the collection you're using, and what you're trying to do.
[ January 02, 2005: Message edited by: Jim Yingst ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that Iterator is an interface, and there are many different implementations within the Java APIs. Each implementation can choose whether or not to implement remove(). If you're getting that exception, then the implementation used in your particular situation doesn't support remove(). The only thing you can really do is to use a different collection class, one whose iterator does support remove().
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys,

I have an array and I convert it to a list using Arrays.asList(), I guess thats why remove() is not supported. I guess I really need to transform that
array into a collection that supports remove method? You guys' posts are very helpful.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although it's not the most efficient way to do things, you could say

List myCollection = new ArrayList(Arrays.asList(myArray));
 
reply
    Bookmark Topic Watch Topic
  • New Topic