Question

I have a nested ArrayList of the form

ArrayList<ArrayList<PointF>> nestedArraylist

I want to create a "copy" nestedArraylistCopy of nestedArraylist in the following sense:

The elements of nestedArraylistCopyshould be independent copies of the elements in nestedArraylist, i.e. should be ArrayLists holding the references to the same PointF objects in the original nestedArraylist.

  • Can I somehow use Collections.copy(dest, src) to do what I want? The documentation is not exactly detailed unfortunately...

  • Does the following code do what I want?

    for(int i = 0; i < nestedArraylist.size(); i++) 
        nestedArraylistCopy.add(new ArrayList<PointF>(nestedArraylist.get(i)));
    
  • Is there a more efficient and or elegant solution?

Was it helpful?

Solution

Q1: after you Collections.copy, your new List object will contain the same elements as src (assuming the size is the same) which means, it holds same ArrayList<PointF> objects, hence the PointF objects are the same too. If you cannot get the info you want from the api java doc, read the source codes.

Q2: What you did is different from Collections.copy, since your copied arrayList has new Arraylist<PointF> as elements, but they contain the same elements (PointF) as the source list.

Q3: I don't know. because I don't know what do you want to have eventually. All new objects? Only ArrayList should be new objects, or all references?

OTHER TIPS

According to my knowledge your solutions will update only references, as does Collection.copy(). You can use the below method, which I prefer:

 List<ArrayList<PointF>> newList = new ArrayList<ArrayList<PointF>>(oldList);

A change of the old list would not affected to new list.

I tested your second option and it also has the property that changes to the old one will not affect the new List.

Note - These will also update only the references. If you change elements in your new list it will update old list too. I think Your second array will create brand new objects, but I am not 100% sure about that. I am adding my testing code below for you reference.

package test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lasitha Benaragama on 4/28/14.
 */
public class StckTest {

public static void main(String[] args) {
    List<String> oldList = new ArrayList<String>();
    oldList.add("AAAAA");
    oldList.add("BBBBB");
    oldList.add("CCCCC");
    oldList.add("DDDDDD");
    oldList.add("EEEEEE");

    StckTest test = new StckTest();

    List<String> newListCopy = new ArrayList<String>(oldList);
    List<String> newListClone = new ArrayList<String>();

    for(int i = 0; i < oldList.size(); i++)
        newListClone.add(oldList.get(i));

    test.printArray(newListCopy);

    test.changeList(oldList);
    test.printArray(oldList);

    test.printArray(newListCopy);
    test.printArray(newListClone);
}

public void changeList(List<String> oldList) {
    oldList.remove(2);
    oldList.add("FFFFF");
}

public void printArray(List<String> oldList){
    for(int i = 0; i < oldList.size(); i++){
        System.out.print(oldList.get(i)+",");
    }
    System.out.println();
}

}

You can use the clone() method to make a shallow copy of your object.

        ArrayList<ArrayList<String>> nestedArraylist=new ArrayList<ArrayList<String>>();
        ArrayList<String> x=new ArrayList<String>();
        x.add("Deepak");
        nestedArraylist.add(x);
        System.out.println(nestedArraylist);
        ArrayList<ArrayList<String>> nestedArraylistcopy=(ArrayList<ArrayList<String>>)nestedArraylist.clone();
        System.out.println(nestedArraylistcopy);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top