Community
Participate
Working Groups
package array; import java.util.*; public class ArrayListTest { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("Mike"); al.add("John"); al.add("David"); // ArrayList toArray() returns an Object[] array. Object[] objs = al.toArray(); // fixed List!!! be careful. List l = Arrays.asList(objs); l.add("Eric"); /* * Arrays asList() returns a fixed-size list * Eclipse 3.3 doesn't flag a error for the above l.add("Eric"). * so I got an exception during the execution * Exception in thread "main" * java.lang.UnsupportedOperationException at * java.util.AbstractList.add(Unknown Source) at * java.util.AbstractList.add(Unknown Source) at * array.ArrayListTest.main(ArrayListTest.java:17) * */ System.out.println(l); } }
You don't know from the type (List) that it is fixed size. It is just as likely to be a variable-sized one. So unless you can track where the collection is instantiated, you won't find it in the general case. It doesn't sound sensible to me to have a bunch of functionality that's associated with a single call to an API, but that's just my personal opinion.
This is not up to the compiler to know the semantic of the method Arrays.asList(...). It is up to the developer to know that the returned list is a fixed-size list. Closing as INVALID.
Verified for 3.4 M2 using build I20070917-0010.