0

I was implementing a program to remove the duplicates from the 2 character array. I implemented these 2 solutions, Solution 1 worked fine, but Solution 2 given me UnSupportedOperationException. Why is that so? The error is at line al1.removeAll(al2);

public void getDiffernce(Character[] inp1, Character[] inp2){

    // SOLUTION 1:
    // **********************************************

    List<Character> list1 = new ArrayList<Character>(Arrays.asList(inp1));
    List<Character> list2 = new ArrayList<Character>(Arrays.asList(inp2));
    list1.removeAll(list2);

    System.out.println(list1);
    System.out.println("***************************************");


    // SOLUTION 2:

    Character a[] = {'f', 'x', 'l', 'b', 'y'};
    Character b[] = {'x', 'b','d'};

    List<Character> al1 = new ArrayList<Character>(); 
    List<Character> al2 = new ArrayList<Character>(); 
    al1 = (Arrays.asList(a)); System.out.println(al1);
    al2 = (Arrays.asList(b)); System.out.println(al2);  
    al1.removeAll(al2);         // error is here     
    System.out.println(al1);
}
3
  • 2
    What is the line number referenced in the exception stacktrace? Which line here does it correspond to? Commented Jun 26, 2013 at 15:04
  • 3
    Please do not cross post to different sites on stack exchange. Commented Jun 26, 2013 at 15:05
  • It says Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144) at java.util.AbstractList$Itr.remove(AbstractList.java:360) at java.util.AbstractCollection.removeAll(AbstractCollection.java:337) at dominator.Dominator.getDiffernce(Dominator.java:111) at dominator.Dominator.main(Dominator.java:23) Commented Jun 26, 2013 at 15:07

3 Answers 3

3

From the asList(..) documentation:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

Basically the asList method does not create a normal mutable List<E> but a wrapper around an array. The remove operation is hence not available on a list which is backed by an array.

Try to build your lists by manually constructing them:

List<Character> al1 = new ArrayList<Character>(Arrays.asList(a)) 

So that the backed list is used only to initialize a real ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

3

Arrays.asList() returns a fixed-size list. No entries can be added or removed.

You can work around this by:

al1 = new ArrayList<Character>((Arrays.asList(a))); 

and the same for al2.

This is already happening in the top solution.

in the second, you create an ArrayList then overwrite it with the fixed one, instead of filling it, keeping in mutable.

3 Comments

al1.addAll((Arrays.asList(a))); al2.addAll((Arrays.asList(b))); The addAll() worked for me.
@JNL I'm proposing another method. That is also valid.
True...Agree with your method. It just got me thinking about the addAll() method. Nonetheless all the above answers helped me to learn more about the method. Thanks
0

You use Arrays.asList(). And this type of list is not modifiable.

The javadoc explicitly says so:

Returns a fixed-size list backed by the specified array. [emphasis mine]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.