Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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数组的克隆中删除对象_Java_Arrays_Arraylist - Fatal编程技术网

Java 从ArrayList数组的克隆中删除对象

Java 从ArrayList数组的克隆中删除对象,java,arrays,arraylist,Java,Arrays,Arraylist,我的问题很基本,但我仍然不知道如何做到这一点。我有一个ArrayList数组,如下所示: ArrayList<Car>[] cars = new ArrayList[2]; ArrayList[]cars=新的ArrayList[2]; 稍后在我的程序中,我将迭代该数组,需要从ArrayList中删除一些对象。但是,由于我无法迭代列表并同时删除元素,因此我决定克隆数组并从克隆的ArrayList中删除对象。不过我注意到,克隆引用的对象与数组中的对象相同。以下是一些基本示例:

我的问题很基本,但我仍然不知道如何做到这一点。我有一个ArrayList数组,如下所示:

ArrayList<Car>[] cars = new ArrayList[2];
ArrayList[]cars=新的ArrayList[2];
稍后在我的程序中,我将迭代该数组,需要从ArrayList中删除一些对象。但是,由于我无法迭代列表并同时删除元素,因此我决定克隆数组并从克隆的ArrayList中删除对象。不过我注意到,克隆引用的对象与数组中的对象相同。以下是一些基本示例:

    ArrayList<Car>[] cars = new ArrayList[2];
    cars[0] = new ArrayList<Car>();
    cars[0].add(new Car("black"));
    cars[0].add(new Car("white"));

    cars[1] = new ArrayList<Car>();
    cars[1].add(new Car("green"));
    cars[1].add(new Car("red"));

    ArrayList<Car>[] carClone = cars.clone();

    carClone[0].remove(0); // removes the objects from the ArrayList of clone as well as the ArrayList of the "real" array
ArrayList[]cars=新的ArrayList[2];
cars[0]=新的ArrayList();
汽车[0]。添加(新车(“黑色”);
汽车[0]。添加(新车(“白色”);
cars[1]=新的ArrayList();
汽车[1]。添加(新车(“绿色”));
汽车[1]。添加(新车(“红色”));
ArrayList[]carClone=cars.clone();
carClone[0]。删除(0);//从克隆的ArrayList和“真实”数组的ArrayList中删除对象

在上面的示例中,您可以看到它不仅从克隆中删除对象,还从阵列中删除对象。我怎样才能克服这个问题?有什么想法吗?

您想从
ArrayList
中删除元素的依据是什么?也看到了为什么你需要一系列的列表?也许最好使用列表列表