T
T
Timur Sergeevich2016-04-06 09:04:07
Java
Timur Sergeevich, 2016-04-06 09:04:07

Why is an UnsupportedOperationException thrown?

List<Integer> list1 = Arrays.asList(10,20,60,30,22,70,89);

        List<Integer> list2 = Arrays.asList(1,2,45,23,89,66,87,33,19,39);

        //list1.forEach(System.out::println);

        list1.addAll(list2);


Exception in thread "main" java.lang.UnsupportedOperationException // Throws addAll()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dorofeev, 2016-04-06
@MyAlesya

Array.asList()is just a wrapper over an array with an interface List. And arrays have a fixed size, so adding
and removing elements is not supported. If you want to fill with Listvalues ​​from an array , then do this:
List list = new ArrayList(Arrays.asList(1,2,3));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question