Zhe
Zhe

Reputation: 1

How to add Arraylist into another Arraylist in Java

I have one question about adding one Arraylist of Strings into another Arraylist, the code is the following:

public List<List<Integer>> combinationSum(int[] candidates,int target){
    List<List<Integer>> res=new ArrayList<>();
    Arrays.sort(candidates);
    List<Integer>tempList=new ArrayList<>();
    backtrack(res,tempList,candidates,target,0);
    return res;

}

public void backtrack(List<List<Integer>>res, List<Integer>tempList,
                      int[]nums, int remain, int start){
    if(remain==0) res.add(new ArrayList<>(tempList));
    if(remain<0) return;
    for(int i=start;i<nums.length;i++){
        tempList.add(nums[i]);
        backtrack(res,tempList,nums,remain-nums[i],i);
        tempList.remove(tempList.size()-1);
    }
}

I am wondering in the function of "backtrack", why we need to use the new Arraylist(tempList) when we want to add the tempList into the res Arraylist. Why can not we just put the tempList into res, like if(remain==0) res.add(tempList),since I thought tempList was already declared as an arraylist before and passed as one argument. Thank you.

Upvotes: 0

Views: 346

Answers (1)

RobOhRob
RobOhRob

Reputation: 585

What this line res.add(new ArrayList<>(tempList)); does, is create a new ArrayList with the same contents as tempList. This allows you to add/remove elements on tempList without it affecting the shallow copy added into res

public static void main( String[] args ) {

    List<String> listA = new ArrayList<>();
    listA.add( "listA" );
    listA.add( "listAA" );

    List<String> listB = new ArrayList<>();
    listB.add( "listB" );
    listB.add( "listBB" );

    // add both our lists above to a new list of lists
    List<List<String>> manyLists = new ArrayList<>();
    manyLists.add( listA );
    manyLists.add( new ArrayList<>( listB ) );

    // clear the contents of both list A and list B
    listA.clear();
    listB.clear();

    // both of these will be empty because we simply added the reference for listA when we did `manyLists.add( listA );`
    System.out.println( "listA contents -> " + listA );
    System.out.println( "listA contents in many lists -> " + manyLists.get( 0 ) );
    System.out.println();

    // listB will be empty, but manyLists.get( 1 ) will not, because we created a copy of listB (a new Object) before adding it to our manyLists
    // So any changes to listB won't affect our new ArrayList in manyLists.get( 1 )
    // NOTE: This does not make a copy of the contents (Objects) in listB (this is only a shallow copy)
    // those still point to the original references (in this case Strings)
    System.out.println( "listB contents -> " + listB );
    System.out.println( "listB contents in many lists -> " + manyLists.get( 1 ) );

}


// Output:

// listA contents -> []
// listA contents in many lists -> []

// listB contents -> []
// listB contents in many lists -> [listB, listBB]

Upvotes: 1

Related Questions