Array Conversion To List Error

Viewed 142

could someone explain to me what happens in the next lines of code and why it works?

Integer[] myray = {1,2,3,4,5};
List<Integer> l = new ArrayList<Integer>(Arrays.asList(myray));
l.add(6);
System.out.println(l);

The code above works fine. It converts an array to a list and then adds another element. But the following code dose not work

Integer[] myray = {1,2,3,4,5};
List<Integer> l = (Arrays.asList(myray));
l.add(6);
System.out.println(l);

The above code gives me the following error: Exception in thread "main" java.lang.UnsupportedOperationException Can someone please tell me the difference between the two conversions and why only the first one works?? Thanks in advance

2 Answers
Related