Hi Saran,
Integer i={3,5}
List l=Arrays.asList(i);
l.set(1,9);
asList method returns list,but list is a interface then how can we invoke a method set on it.
Think about the line:
List<
String> alist = new ArrayList<String>();
alist.add("Hello");
alist.add("howdy");
alist.add("hi");
alist.set(1,"rowdy"); // Can we do this? Absolutely you can
Now your question:
When you are using set(), it is not going to add a new object to the list,
it is just modification, but add() will add a new object so this is not allowed.
What asList() returns is fixed List. java.lang.UnSupportedOperationException is thrown at run time when you apply
add() method to it.
Note: What warning you are getting is not due to the add() method.
Actually your List reference "l" is raw type. It should be parameterized
as :
List<Integer> l1;
Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]