Solved

UnsupportedOperationException while removing element from List

Posted on 2008-06-10
8
1,252 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 250 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
 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 250 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now