?
Solved

UnsupportedOperationException while removing element from List

Posted on 2008-06-10
8
Medium Priority
?
1,261 Views
Last Modified: 2008-06-10
Hello

 i have a List<File> queryPool;

 in findImage() method, I loop thru the queryPool and find one image called queryImage of type File, which i return to main function

 and while trying to remove this from the queryPool -

queryPool.remove(queryImage);

it throws the java.lang.UnsupportedOperationException .. i tried using Iterator over the queryPool, still error

any comments?
protected File findImage() {
 // some calculations
 
 for(File f : queryPool) {
   // calculations
 
   if(closeValues.get(f) < temp2) {
	queryImage = f;
   }
 }
 
return queryImage;
}

Open in new window

0
Comment
Question by:max_dub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 21751430
If you want to remove it while iterating, you should


a. clone the File to return to the caller
b. Use Iterator explicitly and call remove on it
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 21751448
once you have created an iterator for a collection you should explicitly use the add and remove methods from the iterator. if you try to add or remove from the collection directly you will get the exception.
0
 

Author Comment

by:max_dub
ID: 21751479
"explicitly use the add and remove methods" - can u explain this bit
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 1000 total points
ID: 21751539
check your list implementation,
if you are trying to get the list using Arrays.asList(), then the returned list do not support
the remove,

since it is java.util.Arrays$ArrayList and not java.util.ArrayList
chekc the class with list.getClass()
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 21751559
if you use an iterator and try to remove the object from the list, it should throw a ConcurrentModificationException and not UnSuportedOperationExcepotion,

UnSupported..Exception is thrown by the remove() method in the AbstractList(), if the implementation of this class do no impleemnt remove() then u will get that exception

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21751628
This is the sort of thing you need to do
        List<File> fileList = new ArrayList(Arrays.asList(new File(".").listFiles()));
        Iterator<File> i = fileList.iterator();
        String copied = null;
        while (i.hasNext()) {
            File f = i.next();
            if (f.getName().equals("b")) {
                copied = f.getName();
                i.remove();
            }
        }
        System.out.println(fileList);
        System.out.println(copied);

Open in new window

0
 

Author Comment

by:max_dub
ID: 21751748
thanks guys..

i was missing the remove function from the List implementation from an external jar i was using.. i added the remove function there n it works

cheers
0
 

Author Comment

by:max_dub
ID: 21751756
ah, sorry i wanted the CEHJ answer to be assisted solution, but it turned out other way around .. always .. thanks mukundha_expert for ur solution
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question