| Author |
iterator remove method
|
Prahlad Joshi
Ranch Hand
Joined: Apr 21, 2007
Posts: 44
|
|
I am not able to understand why the above code is generating the following exception. Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144) at java.util.AbstractList$Itr.remove(AbstractList.java:360)
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
It's a very good question. Lets do it step by step.
List< String>words=Arrays.asList("this","and","that");
You know what Arrays.asList() returns? 1. It returns an ArrayList.
public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); }
But this ArrayList is different from our normal java.util.ArrayList. 2. This ArrayList is private static inner class on the Arrays class.
3. This inner ArrayList defined remove(int) method.
Actually this inner ArrayList inherits this method from AbstractList. 4. So, when you call
it.remove();
then above remove(int) functionality is called internally that throws the given exception. 5. Suppose it was java.util.ArrayList, then ArrayList defined its own remove(int) method, that is internally called by Iterator.remove().
public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; }
then you would not have got UnsupportedOperationException. [ December 21, 2008: Message edited by: punit singh ]
|
SCJP 6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
As for the "why" it is not supported... List<String>words=Arrays.asList("this","and","that"); The Arrays class is meant to work with arrays. This is obfuscated a bit here, as it just looks like you are passing in three strings. But this is because this method can also accept the input as an vararg -- in fact, it is just passed as an array of size three, with those three strings. The purpose of this method is to return a list, that is backed with the array. Meaning, if you change the list, the original array changes. If you change the array, the list will change. Unfortunately, this means that you can't add or remove items from the list. An array can't change the number of elements. Hence, with this list, you also can't change the number of elements. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Prahlad Joshi
Ranch Hand
Joined: Apr 21, 2007
Posts: 44
|
|
|
Thanks Henry and Punit for your clear and detailed explanations.It proved to be a great help for me.
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
One more thing Prahlad, you cannot add, remove but you can modify the content of array: And take care content modification method will work like:
list.set(index,value);
but size modification methods will not work like:
list.add(index,value); list.add(value);
|
 |
 |
|
|
subject: iterator remove method
|
|
|