• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
java Arrays.asList方法注意事项

1. 简介

Arrays.asList()方法可以将数组转化为长度固定的列表。

该方法强调了列表的长度是固定的,因此不能使用list的add和remove方法修改list长度。

2. 示例

 1 import java.util.*;
 2 
 3 public class ListFunc1 {
 4     public static void main(String[] args){
 5 
 6         Integer[] array1 = new Integer[]{1,2,3,4};
 7         List<Integer> list1 = Arrays.asList(array1);
 8         list1.set(0,0);
 9 //        list1.add(5);                  // (1)
10 //        list1.remove(2);              // (2)
11         System.out.println(list1);
12 
13         /*
14         * ArrayList(Collection<? extends E> c)
15         * 按照集合的迭代器返回的顺序构造一个包含指定集合元素的列表
16         * */
17         Integer[] array2 = new Integer[]{1,2,3,4};
18         List<Integer> list2 = new ArrayList<>(Arrays.asList(array2));   // (3)
19         list2.set(0,0); 
20         list2.add(5);
21         list2.remove(2);
22         System.out.println(list2);    // [1, 2, 4, 5]
23     }
24 }

示例说明如下:

(1)使用add()方法时将会抛出异常:Exception in thread "main" java.lang.UnsupportedOperationException;

         源码如下,可以发现使用add方法时将会直接抛出异常。

1  public boolean add(E e) {
2         add(size(), e);
3         return true;
4  }
5 
6 public void add(int index, E element) {
7         throw new UnsupportedOperationException();
8 }

(2)使用remove()方法时将会抛出异常:Exception in thread "main" java.lang.UnsupportedOperationException;

         源码如下,可以发现使用remove方法时将会直接抛出异常。

1 public E remove(int index) {
2         throw new UnsupportedOperationException();
3 }

(3)如果想要修改由数组转换而成的列表,可以使用ArrayList(Collection<? extends E> c)构造器,

        新建一个列表即可,构造器的参数为集合。

 1 import java.util.*;
 2 
 3 public class Main1 {
 4     public static void main(String[] args){
 5 
 6         /* Arrays.asList()构建列表
 7         * 列表长度不可变,即不可以add和remove
 8         * */
 9         int[] intArray1 = new int[]{5, 7};
10         List<int[]> intList1 = Arrays.asList(intArray1);
11         System.out.println(intList1);   // [[I@1540e19d]
12 
13         Integer[] intArray2 = new Integer[]{5, 7};
14         List<Integer> intList2 = Arrays.asList(intArray2);
15         //        intList2.add(3);              // error
16         System.out.println(intList2);   // [5, 7]
17 
18 
19 
20         /* 使用ArrayList(Collection<? extends E> c)构建列表
21         *  长度可变,即可以add和remove
22         * */
23         int[] intArray = new int[]{2, 4};
24         List<int[]> intList = new ArrayList<>(Arrays.asList(intArray));
25         System.out.println(intList);    // [[I@1540e19d]
26 
27         Integer[] integerArray = new Integer[]{1,3};
28         List<Integer> integerList = new ArrayList<>(Arrays.asList(integerArray));
29         integerList.add(5);          // success
30         System.out.println(integerList);   // [1, 3, 5]
31     }
32 }

!!!

posted on 2019-04-10 10:50  火磷  阅读(390)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3