Solved

UnsupportedOperationException while removing element from List

Posted on 2008-06-10
8
1,253 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 50
ArrayIndexOutOfBoundException 9 68
Java - Why doesn't this JFrame work 3 42
oracle 11g 23 50
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 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

15 Experts available now in Live!

Get 1:1 Help Now