The role of creating a new collection object when a collection is passed as a function parameter

List collection error-prone

When using List collections to nest collections , it is common to see code like this:

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

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

You may be wondering why not res.add(list)? Is thisnew ArrayList(list) redundant? Let's simply write down the code and see the results:
1. The first way of writing

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. The second way of writing

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]
	}	
}

The results are found to be the same, but are they really the same? Let's perform some operations on the list collection to continue testing

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: []
	}	
}

The operation of clearing the list will find that the results of the two writing methods are no longer the same. The reason for this is that res.add(list)this writing method is to add the list object directly to the res collection. At this time, our modification to the list collection will also change the res The value of the list collection added to the collection.

If it is res.add(new ArrayList <Integer> (list));a new set added to the res set at this time that has the same data elements as the data elements stored in the list set, then modifying the list set at this time will not affect the res set.

It can be seen that a collection is an object that corresponds to an address value. The elements stored in different collections may be the same, but the corresponding address values ​​are different. If a set is modified, the element data corresponding to the set with this set as the element data will also be modified (the address value is the same).

Guess you like

Origin blog.csdn.net/qq_52595134/article/details/122628670