集合作为函数参数传参时创建新集合对象的作用

简介: 易错点——List集合集合作为函数参数传参时创建新集合对象的作用

List集合易错点

在使用List集合嵌套集合时,经常会看到这样的代码:

List<Integer> list = new ArrayList<Integer>();
        
        //一些对list集合的操作
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();

        res.add(new ArrayList<Integer>(list));

大家可能会有疑问 为什么不是res.add(list)呢? 这样再new ArrayList(list)是否多余呢? 我们先来简单写下代码,看看结果:
1.第一种写法

class Main{
public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        res.add(list);    
        System.out.println("res: "+res);
        System.out.println("list: "+list);
        
    //运行结果    
    //       res: [[1, 2]]
    //     list: [1, 2]
    }    
}

2.第二种写法

class Main{
public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        res.add(new ArrayList<Integer>(list));    
        System.out.println("res: "+res);
        System.out.println("list: "+list);
        
    //运行结果    
    //       res: [[1, 2]]
    //     list: [1, 2]
    }    
}

发现结果是相同的,但是真的相同吗?我们再对list集合进行一些操作继续测试

class Main{
public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        res.add(list);    
        list.clear();
        System.out.println("res: "+res);
        System.out.println("list: "+list);
        
    //运行结果    
    //       res: [[]]
    //     list: []
    }    
}
class Main{
public static void main(String[] args) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        res.add(new ArrayList<Integer>(list));        
        list.clear();
        System.out.println("res: "+res);
        System.out.println("list: "+list);
        
    //运行结果    
    //  res: [[1, 2]]
   //   list: []
    }    
}

对list进行清空的操作会发现两种写法的结果不再相同了,出现这样的原因在于,res.add(list) 这种写法是将list对象直接添加到res集合中,此时我们对list集合的修改也会改变res集合中所添加list集合的值。

而如果是res.add(new ArrayList <Integer> (list)); 此时res集合添加的一个新的与list集合所存储数据元素相同的集合,这个时候再对list集合进行修改是不会影响res集合的。

由此可见,一个集合是一个对象也就是对应一个地址值,不同集合存储的元素可能相同,但是对应地址值不同。如果对一个集合进行修改,那么以这个集合作为元素数据的集合所对应的元素数据也会被修改(地址值相同)。

相关文章
|
3月前
多层嵌套对象的解构赋值时,如果对象的属性名相同怎么办?
在多层嵌套对象的解构赋值中遇到属性名相同时,使用别名是一种有效的解决方法,它能够帮助我们准确地提取和使用对象中的数据,避免变量名冲突,提高代码的质量和可维护性。
79 7
|
存储 JSON PHP
PHPJSON嵌套对象和数组的解析方法
在PHP编程开发中,JSON是一种非常常用的数据格式。它具有简单、轻量和易于解析的特点,非常适合用于数据交换和存储。当我们处理JSON数据时,经常需要解析嵌套的对象和数组,本文将介绍几种解析方法。
116 1
|
9月前
|
JavaScript 前端开发 索引
往数组添加对象的方法
往数组添加对象的方法
39 0
|
9月前
2020-10-10 数组和对象的区分方法
2020-10-10 数组和对象的区分方法
|
存储 索引
数组和对象有什么区别?
数组和对象有什么区别?
99 0
|
存储 JavaScript 前端开发
什么是数组,什么是对象,他们的区别是什么
什么是数组,什么是对象,他们的区别是什么
81 0
对象定义-解构-枚举属性遍历以及对象内函数
对象定义-解构-枚举属性遍历以及对象内函数
80 0
lodash创建自身和继承的可枚举属性的值为数组
lodash创建自身和继承的可枚举属性的值为数组
92 0
|
C++
C++ 用引用的方式向函数传递数组
C++ 用引用的方式向函数传递数组
156 0
C++ 用引用的方式向函数传递数组
|
C语言 Android开发 C++
【C++】函数 指针类型参数 与 引用类型参数 对比 ( 修改外部变量需要传入的参数要求 | 参数作返回值 )
【C++】函数 指针类型参数 与 引用类型参数 对比 ( 修改外部变量需要传入的参数要求 | 参数作返回值 )
199 0