1

Can anyone help me to get the issue ?

public static void main(String[] args) 
{
    List<TestEnum> list1 = new ArrayList<TestEnum>();
    list1.add(TestEnum.ONE);
    list1.add(TestEnum.TWO);
    list1.add(TestEnum.THREE);

    System.out.println(list1);
    System.out.println(list1.remove(TestEnum.TWO));
    System.out.println(list1);

    System.out.println("-----------------------");

    TestEnum[] xx = new TestEnum[]{TestEnum.ONE, TestEnum.TWO, TestEnum.THREE};
    List<TestEnum> list2 = Arrays.asList(xx);

    System.out.println(list2);
    System.out.println(list2.remove(TestEnum.TWO));
    System.out.println(list2);
}

Below is the Result -

[ONE, TWO, THREE]
true
[ONE, THREE]
-----------------------
[ONE, TWO, THREE]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at java.util.AbstractCollection.remove(Unknown Source)
at Test.main(Test.java:24)

Please help - Why this is happening, I have also checked hashcode ?

Bharat
  • 750
  • 1
  • 9
  • 20
  • 1
    list returned by `Arrays.asList()` doesn't support `.remove()` – Alex Salauyou Apr 01 '16 at 11:44
  • 1
    Possible duplicate of [Difference between Arrays.asList(array) vs new ArrayList(Arrays.asList(ia)) in java](http://stackoverflow.com/questions/16748030/difference-between-arrays-aslistarray-vs-new-arraylistintegerarrays-aslist) – jhamon Apr 01 '16 at 11:44

4 Answers4

14

List returned by Arrays.asList() has fixed size--the size of array it is backed by. It doesn't support operations that may lead to size change: add(), addAll(), remove(), removeAll(), retainAll(), clear().

You may use the following idiom to create a list of modifiable size:

List<TestEnum> = new ArrayList<>(Arrays.asList(xx));

this will create an ordinary ArrayList initialized by elements copied from result of Arrays.asList().

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
2

Arrays.asList returns instance of immutable list, on which remove cannot be called. You have to wrap it in new list

new ArrayList(Arrays.asList(xx))

sidgate
  • 14,650
  • 11
  • 68
  • 119
2

If you take a look into the method asList of the Arrays class, you will see that a new ArrayList<>() is returned. But the object which is instantied there is another private ArrayList class from inside the Arrays class. This class extends AbstractList like the normal ArrayList does, but it does not override the remove method. And this is the original implementation of the remove method.

/**
 * {@inheritDoc}
 *
 * <p>This implementation always throws an
 * {@code UnsupportedOperationException}.
 *
 * @throws UnsupportedOperationException {@inheritDoc}
 * @throws IndexOutOfBoundsException     {@inheritDoc}
 */
public E remove(int index) {
    throw new UnsupportedOperationException();
}
kai
  • 6,702
  • 22
  • 38
1

Arrays.asList returns a special ArrayList object which does not support any operation which would modify it's size.

From the documentation of this method:

This method also provides a convenient way to create a fixed-size list initialized to contain several elements.

justAbit
  • 4,226
  • 2
  • 19
  • 34