Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将Arraylist添加到另一个Arraylist中_Java_Arraylist - Fatal编程技术网

如何在Java中将Arraylist添加到另一个Arraylist中

如何在Java中将Arraylist添加到另一个Arraylist中,java,arraylist,Java,Arraylist,我有一个关于将字符串的一个Arraylist添加到另一个Arraylist的问题,代码如下: public List<List<Integer>> combinationSum(int[] candidates,int target){ List<List<Integer>> res=new ArrayList<>(); Arrays.sort(candidates); List<Integer>te

我有一个关于将字符串的一个Arraylist添加到另一个Arraylist的问题,代码如下:

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);
    }
}
公共列表组合总和(int[]候选项,int目标){
List res=new ArrayList();
数组。排序(候选);
ListtempList=newArrayList();
回溯(res,templast,候选者,目标,0);
返回res;
}
public void backtrack(Listres、listemplast、,
int[]nums,int剩余,int开始){
if(remain==0)res.add(newarraylist(templast));

如果(保持这一行
res.add(new ArrayList(templast));
所做的是创建一个与templast内容相同的新ArrayList。这允许您在templast上添加/删除元素,而不会影响添加到
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]
publicstaticvoidmain(字符串[]args){
List listA=new ArrayList();
listA.添加(“listA”);
listA.添加(“listAA”);
List listB=新的ArrayList();
列表B.添加(“列表B”);
列表B.添加(“列表BB”);
//将以上两个列表添加到新列表中
List manyLists=new ArrayList();
manyLists.add(listA);
添加(新的ArrayList(listB));
//清除列表A和列表B的内容
listA.clear();
listB.clear();
//这两个都将是空的,因为我们在执行`manyLists.add(listA)时只是添加了对listA的引用`
System.out.println(“listA内容->”+listA);
System.out.println(“许多列表中的listA内容->”+manyLists.get(0));
System.out.println();
//listB将为空,但manyLists.get(1)不会为空,因为我们在将listB(一个新对象)添加到manyLists之前创建了它的副本
//因此,对listB的任何更改都不会影响我们在ManyList中的新ArrayList.get(1)
//注意:这不会复制listB中的内容(对象)(这只是一个浅拷贝)
//这些仍然指向原始引用(在本例中为字符串)
System.out.println(“listB内容->”+listB);
System.out.println(“许多列表中的listB内容->”+manyLists.get(1));
}
//输出:
//listA内容->[]
//列出许多列表中的内容->[]
//listB内容->[]
//许多列表中的listB内容->[listB,listBB]

如果你试图直接把它放在那里会发生什么?很难理解这里到底发生了什么…但是看起来,
新的Arraylist(tempList)
正在创建一个你的tempList的副本…这样添加到res的tempList会保留所有原始引用,并且不会被tempList.add()和tempList.remove()更改非常感谢您,您的解释非常清楚,我理解其中的区别。@Zhe没问题,请随意接受,这样我可以得到一些分数