-1

I have a problem to deep copy an ArrayList containing Attribute objects. After I have copied the ArrayList dataSet in a new one called trainingSet, I am trying to clear (of the trainingSet) all the content of the internal ArrayList of the Attribute called data. When I do so all the same content of the the ArrayList dataSet (data of dataSet) gets cleared, too. So in that case I have tried to deep copy all the content of the original list to the new one using the below tuts:

  1. http://javarevisited.blogspot.gr/2014/03/how-to-clone-collection-in-java-deep-copy-vs-shallow.html#axzz4ybComIhC

  2. https://beginnersbook.com/2013/12/how-to-clone-an-arraylist-to-another-arraylist/

  3. How to make a deep copy of Java ArrayList

but I got the same behavior. So can someone please tell me how I can fix this problem and where the wrong thinking is?

Thank you for help.

ID3Algorithm.java

...
ArrayList<Attribute> dataSet = new ArrayList<dataSet>();
ArrayList<Attribute> trainingSet = new ArrayList<Attribute>(dataSet);

for(Attribute att : trainingSet) {
  att.GetData().clear();  // At this point all the data in dataSet are cleared,too.
}
...

Attribute.java

public class Attribute
{
  private String name;  
  private ArrayList<String> branchNames = new ArrayList<String>();      
  private ArrayList<String> data = new ArrayList<String>(); 
  private ArrayList<Branch> branches = new ArrayList<Branch>(); 
  private HashMap<String, Integer> classes = new HashMap<String, Integer>();
  private ID3Algorithm id3;
  private Leaf leaf = null;

  public ArrayList<String> GetData() { return data; }

  public Attribute(String attribName, ArrayList<String> attribBranchNames, ArrayList<String> attribData, ID3Algorithm algo) {
    name = attribName;
    branchNames = attribBranchNames;
    data = attribData;
    id3 = algo;             
  }
  ...
}
Mike
  • 37
  • 2
  • 7

1 Answers1

0

When you are assigning a value to trainingSet

ArrayList<Attribute> trainingSet = new ArrayList<Attribute>(dataSet);

You are only passing the references for the existing attributes into a new list. It is not a new list of different attribute objects. The first link you post, describes this process in detail. I would re-read it in depth.(The first example is a shallow copy)

http://javarevisited.blogspot.gr/2014/03/how-to-clone-collection-in-java-deep-copy-vs-shallow.html#axzz4ybComIhC

So when you call

att.GetData().clear();

You are clearing the orginal attribute objects data (which dataset also references)

Try creating new Attribute objects and assigning new data to each(copied from the orginal) then adding those to your trainingSet list.

Berkley Lamb
  • 283
  • 2
  • 12
  • Hi, and thanks for your help. I have tried the first ones deep copy part, too but it didn't work for me. I have tried your solution, too, to create a new Attribute object and add it to the data list. The problem here is that I have more than one Attributes to create every time and so have to clear every time the temp data ArrayList. When I am doing so, the contents of the temp list get overridden with the new one cause I am making a shallow copy too here. – Mike Nov 17 '17 at 23:26