4

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?

5
  • 3
    Regarding your second question, you can easily test it.. Commented Apr 28, 2014 at 9:30
  • Yes, I can and have tested the second option and think it works. However, I wanted to ask about Collections.copy() and a more elegant solution anyway, so I thought it would make this question more complete and useful. Commented Apr 28, 2014 at 9:38
  • in fact, I don't understand the two bold words in your question, independent and same.... Commented Apr 28, 2014 at 9:49
  • independent = If you modify the elements of nestedArraylist those of nestedArraylistCopy should not change same = If you modify a PointF in one of the ArrayLists in nestedArraylist, then the corresponding PointF in the ArrayLists in nestedArraylistCopy should change. Commented Apr 28, 2014 at 9:55
  • @cgogolin so sounds like you want List<PointF> different objects, but share same PointF ref with source list. then your way in Q2 should be ok. but keep in mind, if you "changed" the PointF in your copied list, the element source list would be changed as well. btw, this could be tested easily. Commented Apr 28, 2014 at 11:36

3 Answers 3

3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining Collections.copy(). I am not an expert, but will have a look into the implementation next time before asking. What you describe under Q2 is exactly I want to achieve. This is what I tried to describe using the words independent and same in my question.
1

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();
}

}

Comments

0

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);

1 Comment

Thanks, but that doesn't really answer my question. I want the elements of nestedArraylistCopy to be be independent copies of those of nestedArraylist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.