使arraylist等于另一个arraylist java

时间:2015-02-12 02:14:36

标签: java eclipse arraylist copy

我有1d临时arraylist和2d原始数组列表,我在循环中更新了临时arraylist,在循环结束时我需要使原始arraylist等于临时arraylist的内容如下:

while(!Front.get(front).isEmpty()){
        Q.clear();
        for(int i=0;i<Front.get(front).size();i++){
            if(!Individual_Dominate.get(Front.get(front).get(i)).isEmpty()){
                for(int j=0;j<Individual_Dominate.get(Front.get(front).get(i)).size();j++){
                    Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]=Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]-1;
                    if(Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]==0){
                        Solutions_to_arrange[Individual_Dominate.get(Front.get(front).get(i)).get(j)][Input.General_Inputs.Num_objectives+Input.General_Inputs.Num_Of_Ppes]=front+1;
                        Q.add(Individual_Dominate.get(Front.get(front).get(i)).get(j));
                    }
                }
            }
        }
        front++;
        Front.get(front)=Q // How to do this step
    }

我知道有像

这样的方法
Front.get(front) = new ArrayList<Object>(Q);

Front.get(front) = (ArrayList<Object>)Q.clone();

但不确定这些方法是否合适,因为所有这些方法都会给我一份我的列表,而不是它的所有元素。因此,如果我更改了复制的List中的一个元素,它也将在我原来的List中更改。

更新 我提出这个解决方案的任何评论?

front++;
for(int i=0;i<Q.size();i++)
        Front.get(front).add(Q.get(i));
    }

2 个答案:

答案 0 :(得分:0)

我认为这会起作用

while(!Front.get(front).isEmpty()){
        Q.clear();
        for(int i=0;i<Front.get(front).size();i++){
            if(!Individual_Dominate.get(Front.get(front).get(i)).isEmpty()){
                for(int j=0;j<Individual_Dominate.get(Front.get(front).get(i)).size();j++){
                    Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]=Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]-1;
                    if(Individual_Number[Individual_Dominate.get(Front.get(front).get(i)).get(j)]==0){
                        Solutions_to_arrange[Individual_Dominate.get(Front.get(front).get(i)).get(j)][Input.General_Inputs.Num_objectives+Input.General_Inputs.Num_Of_Ppes]=front+1;
                        Q.add(Individual_Dominate.get(Front.get(front).get(i)).get(j));
                    }
                }
            }
        }
    front++;
        for(int i=0;i<Q.size();i++)
        Front.get(front).add(Q.get(i));
    }

答案 1 :(得分:0)

这会起作用

 import java.util.ArrayList;

 public class Main {

    public static void main(String[] args) {

        ArrayList<Integer> array = new ArrayList<Integer>();
        ArrayList<Integer> array1 = new ArrayList<Integer>();

        //supposedly you have ArrayList array = [1, 2, 3];

        for(int i : array) {
            array1.add(array.get(i));
        }

        System.out.print(array1);

        // output will give you [1, 2, 3]