deep-copy

java: best way to do deep copy of list of lists

十年热恋 提交于 2020-08-26 13:45:22
问题 I am trying to write a procedure do the deep copy of List<List<Integer>> , and I am doing like this: public static List<List<Integer>> clone(final List<List<Integer>> src) { List<List<Integer>> dest = new ArrayList<List<Integer>>(); for( List<Integer> sublist : src) { List<Integer> temp = new ArrayList<Integer>(); for(Integer val: sublist) { temp.add(val); } dest.add(temp); } return dest ; } Is this a good way to do? Is it possible to get rid of the inner loop? The fact is that each of the

java: best way to do deep copy of list of lists

我只是一个虾纸丫 提交于 2020-08-26 13:45:08
问题 I am trying to write a procedure do the deep copy of List<List<Integer>> , and I am doing like this: public static List<List<Integer>> clone(final List<List<Integer>> src) { List<List<Integer>> dest = new ArrayList<List<Integer>>(); for( List<Integer> sublist : src) { List<Integer> temp = new ArrayList<Integer>(); for(Integer val: sublist) { temp.add(val); } dest.add(temp); } return dest ; } Is this a good way to do? Is it possible to get rid of the inner loop? The fact is that each of the

Making a deep copy of a LinkedList in java

萝らか妹 提交于 2020-08-24 03:45:09
问题 I have a Linked List and I'm trying to create a copy of another Linked List and this copy is a deep copy because the element type is char. Due to the complexity of linked lists, I've tried not to use the add method. My code is shown below. Also I want to recursively add all the elements from some list to my original list but the problem with my implementation is that it only adds the first element in the list and not all of it. Why is this so? public class CharLinkedList { private static

Make a clone of an instanced with all the stored values

ぐ巨炮叔叔 提交于 2020-07-23 09:42:09
问题 I have two classes one called warehouse and one called Warehouselocations. The wareHouse is currently able to create,store and find boxes in warehouselocation. But now i also need the warehouse to be able to create a cloned version of wareHouseLocation with all the stored information. locations = new List<WareHouseLocation>(); This is the list where i store all the information. I want to be able to copy it. I tried to find the answer my self and even tried some code but so far i had got

Make a clone of an instanced with all the stored values

萝らか妹 提交于 2020-07-23 09:40:10
问题 I have two classes one called warehouse and one called Warehouselocations. The wareHouse is currently able to create,store and find boxes in warehouselocation. But now i also need the warehouse to be able to create a cloned version of wareHouseLocation with all the stored information. locations = new List<WareHouseLocation>(); This is the list where i store all the information. I want to be able to copy it. I tried to find the answer my self and even tried some code but so far i had got

How to exclude specific references from deepcopy? [duplicate]

狂风中的少年 提交于 2020-07-09 07:39:30
问题 This question already has answers here : How to override the copy/deepcopy operations for a Python object? (7 answers) Closed 2 years ago . I have object which has it's own content (i.e. list of something) and a reference to another object, with which it is linked. How can I exclude the reference to the other object from being deep-copied? from copy import deepcopy class Foo: def __init__(self, content, linked_to): self.content = content self.linked_to = linked_to a1 = Foo([[1,2],[3,4]], None