Arrays.asList()涉及到数组数量的修改报错

Arrays.asList()返回的是一个内部类,可以对数组进行值的修改
但是,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组

public class Main6 {
    public static void main(String[] args) {
        String[] stringArray = new String[3];
        stringArray[0] = "one";
        stringArray[1] = "two";
        stringArray[2] = "three";

        //Arrays.asList()返回的是一个内部类,可以对数组进行值的修改
        //但是,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组
        List<String> stringList = Arrays.asList(stringArray);
        stringList.set(0,"one-list");
        System.out.println(stringArray[0]);

        //看这3个操作,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组
        stringList.add("four");
        stringList.remove(2);
        stringList.clear();

    }
}

public class Main6 {
    public static void main(String[] args) {
        List masterList = new ArrayList();
        masterList.add("one");
        masterList.add("two");
        masterList.add("three");
        masterList.add("four");
        masterList.add("five");

        List branchList = masterList.subList(0, 3);

        //这3行不注释会导致报错
        //masterList.remove(0);
        //masterList.add("ten");
        //masterList.clear();

        branchList.clear();
        branchList.add("six");
        branchList.add("seven");
        branchList.remove(0);

        //此时 branchList 只剩下一个元素 seven
        //masterList 剩下3个元素,seven  four  five


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值