Java中list集合深复制

原创 健旺  2018-05-29 10:01  阅读 2,297 次
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CopyTest {

    public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
                
        //list深度拷贝
        List<Integer> newList = new ArrayList<>();
        CollectionUtils.addAll(newList, new Object[list.size()]);
        Collections.copy(newList, list);
        newList.set(0, 10);

        System.out.println("原list值:" + list);
        System.out.println("新list值:" + newList);
    }
}

 
原list值:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
新list值:[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]

本文地址:http://www.256it.com/137.html
版权声明:本文为原创文章,版权归 健旺 所有,欢迎分享本文,转载请保留出处!

发表评论


表情