简体   繁体   中英

Adding a array list to arraylist

import java.util.*;
class test2{
    
    static void fun(int i,int arr[],List<Integer> l,int n,List<List<Integer>> res){
        if(i==n){
            if(l.size()!=0){
                //System.out.println(l.size());
                res.add((l));
                //System.out.println(res);
            }
            //System.out.println(l);
            return;
        }
        l.add(arr[i]);
        fun(i+1,arr,l,n,res);
        //System.out.println(l);
        l.remove(l.size()-1);
        //System.out.println(l);
        fun(i+1,arr,l,n,res);
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int arr[]=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        List<Integer> l=new ArrayList<>();
        List<List<Integer>> res=new ArrayList<>();
        fun(0,arr,l,n,res);
        System.out.println(res);
    }
}

在此处输入图像描述

in fun function while i am adding a List to other List it is adding empty list i could find the reason can somebody help me this program is about finding the different combinations of given array

You can use res.add((new ArrayList<>(l)) instead of res.add((l)) .

Why should use (new ArrayList<>(existing_list))?

When you update any primitive value then put in a list you can update again that primivite value. That does not change the value in list. But an object does not work like that. Well we can say it causes kind of pointer. When you update an object then it updates the values in its address. Let me show you a short example.

public class MyObject {


   private List<String> list = new ArrayList<>();

   public MyObject() {
      list.add("added in Constructor");
   }

   public List getList() {
    return list;
   }
}


public static void main(String[] args) {

    MyObject myObject = new MyObject();

    System.out.println("--> Object list (Before added anything in main) : ");
    myObject.getList().forEach(System.out::println);

    List list = myObject.getList();
    list.add("added in Main");

    System.out.println("--> local list : ");
    list.forEach(System.out::println);

    System.out.println("--> Object list (After added a value in main) : ");
    myObject.getList().forEach(System.out::println);
}

And the ouput is:

--> Object list (Before added anything in main) : 
added in Constructor
--> local list :
added in Constructor
added in Main
--> Object list (After added a value in main) : 
added in Constructor
added in Main

I did not set up a new Arraylist or create new one but my private list in MyObject is updated even if I just do changes in main function.

But instead of list If I returned new Array<>(list) then even if I update the list in main, my list would never change. Because I return another address with new Array<>(list) . So you should add your list with another addres. I mean another Arraylist. So you can use res.add((new ArrayList<>(l)) instead of res.add((l)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM