File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes question about Iterator remove method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "question about Iterator remove method" Watch "question about Iterator remove method" New topic
Author

question about Iterator remove method

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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 ]

"I'm not back." - Bill Harding, Twister
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

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().


[Jess in Action][AskingGoodQuestions]
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
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
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

Although it's not the most efficient way to do things, you could say

List myCollection = new ArrayList(Arrays.asList(myArray));
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: question about Iterator remove method
 
Similar Threads
Eliminating A ConcurrentModification Exception
remove method
question about remove() method...
Need your help
EJB 3.0 statefull session bean life cycle dout