作用 : Arrays.asList()
将一个数组转化为一个List对象,这个方法会返回一个ArrayList类型的对象, 这个ArrayList类并非java.util.ArrayList类,而是Arrays类的静态内部类!用这个对象对列表进行添加删除更新操作,就会报UnsupportedOperationException异常。
转:http://blog.youkuaiyun.com/cntanghai/article/details/7188296
用法总结:
- import java.util.Arrays;
- import java.util.List;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class AsListTest {
-
- public static void main(String[] args) {
-
-
-
-
- int[] a_int = { 1, 2, 3, 4 };
-
- List a_int_List = Arrays.asList(a_int);
- foreach(a_int_List);
-
- foreachForBase(a_int_List);
-
-
- Integer[] a_Integer = new Integer[] { 1, 2, 3, 4 };
- List a_Integer_List = Arrays.asList(a_Integer);
- foreach(a_Integer_List);
-
-
- a_Integer_List.set(0, 0);
- foreach(a_Integer_List);
- foreach(a_Integer);
-
- a_Integer[0] = 5;
- foreach(a_Integer_List);
- foreach(a_Integer);
-
-
-
-
-
-
-
-
-
-
- a_int[0] = 5;
- foreachForBase(a_int_List);
- foreach(a_int);
-
- }
-
-
- private static void foreach(List list) {
- for (Object object : list) {
- System.out.print(object + " ");
- }
- System.out.println();
-
- }
-
- private static void foreachForBase(List a_int_List) {
- int[] _a_int = (int[]) a_int_List.get(0);
- foreach(_a_int);
- }
-
- private static void foreach(int[] a_int) {
- for (int i : a_int) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
-
- private static void foreach(Integer[] _a_Integer) {
- for (int i : _a_Integer) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- }