正确使用ArrayList的sublist

本文解析了Java中ArrayList的sublist方法原理,指出其返回的是原列表的视图而非副本,直接修改会导致原列表变化。通过源码分析,提供了解决方案,即创建新的ArrayList来避免影响原列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正确使用ArrayList的sublist

1.问题描述

使用subList方法获取的list,只是一个视图,而不是一个新的list,如果进行操作,会导致原list也变化了。

public class Test {
    public static void main(String[] args){
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add(i);
        }
        for(int i=0;i<10;i++){
            List<Integer> temp=list.subList(0,10);
            System.out.println(temp);
            Iterator<Integer> iterator = temp.iterator();
            while(iterator.hasNext()){
                iterator.next();
                iterator.remove();
            }
        }
        System.out.println(list);
    }
}

打印结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[]

2.源码分析

ArrayList的sublist方法并没有新建一个ArrayList,而是创建了一个原ArrayList的视图。

public List<E> subList(int fromIndex, int toIndex) {
    subListRangeCheck(fromIndex, toIndex, size);
    return new SubList(this, 0, fromIndex, toIndex);
}

这个视图,是一个ArrayList内部类,其实就是用一些坐标指向原ArrayList,所以如果对这个SubList进行操作,也会影响到原ArrayList。

private class SubList extends AbstractList<E> implements RandomAccess {
    private final AbstractList<E> parent;
    private final int parentOffset;
    private final int offset;
    int size;

    SubList(AbstractList<E> parent,
            int offset, int fromIndex, int toIndex) {
        this.parent = parent;
        this.parentOffset = fromIndex;
        this.offset = offset + fromIndex;
        this.size = toIndex - fromIndex;
        this.modCount = ArrayList.this.modCount;
    }
    ......
}

3.解决

可以自己新建一个ArrayList,这样就不会导致上面的问题了。

public class Test {

    public static void main(String[] args){
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add(i);
        }
        for(int i=0;i<10;i++){
            List<Integer> temp=new ArrayList<>(list.subList(10*i,10*(i+1)));
            System.out.println(temp);
            Iterator<Integer> iterator = temp.iterator();
            while(iterator.hasNext()){
                iterator.next();
                iterator.remove();
            }
        }
        System.out.println(list);
    }

}

打印结果

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值