public class ArrayASListTest
{
public static void main(String[] args)
{
/*
String[] s = new String[]{"one","two"};
List wrongList = Arrays.asList(s);
wrongList.remove(0);
*/
String[] s = new String[]{"one","two"};
List rightList = new ArrayList(Arrays.asList(s));
wrongList.remove(0);
}
}
Arrays.asList()生成的List是java.util.Arrays$ArrayList,它的remove和add实现就是抛出UnsupportedOperationException异常。
改正方法也很简单,换成ArrayList或者LinkedList就可以了
转自:http://blog.csdn.net/yinbodotcc/archive/2009/02/18/3907292.aspx
其实API里也说明清楚了:
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
如果仔细看,应该也能避免错误