?
Solved

UnsupportedOperationException while removing element from List

Posted on 2008-06-10
8
Medium Priority
?
1,266 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: 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!

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

839 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