Linked Questions

50 votes
7 answers
224k views

I want to make a copy of an object, then after some logic, re-assign the original object the value of the copy. example: User userCopy = //make a copy foreach(...) { user.Age = 1; user.ID = -1; ...
Blankman's user avatar
  • 269k
3 votes
2 answers
3k views

I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other....
user101's user avatar
  • 175
4 votes
3 answers
997 views

I have a pretty general question about java. I want to know if there is a simple way to recreate this c++ code in java: class A { public: int first; int second; A(const A& other) { *...
kalu's user avatar
  • 169
1 vote
2 answers
2k views

I have an array and i want to create an independent copy of it. Changes in one of those should not impact the other at all. Currently everything i tried just created two references on one object. ...
M.Dietz's user avatar
  • 1,020
1 vote
2 answers
1k views

How can we copy an object in java without copying the reference. If any object is present inside then we should copy the value only with out modifying the original object reference. How can this be ...
vineeth sivan's user avatar
0 votes
1 answer
963 views

I need to know how to for loop through a Hashtable, check for cloneability on each value, and clone if possible. This Hashtable has all String keys, but the values can be of any class. I have come ...
user1567060's user avatar
0 votes
0 answers
129 views

How to deep copy hashmap? I wanna make a copy of hashmap. When I change the keys and values in the copy, the original hashmap doesn't change. I cannot find a solution to solve this problem in the ...
dakongyi's user avatar
1 vote
1 answer
81 views

I'm new at Java and I came across a problem I couldn't find a solution to, so maybe somebody can help me out. When I write this code: ArrayList<String> Liste = new ArrayList<>(); String ...
Tsukeyo's user avatar
  • 13
-3 votes
1 answer
94 views

I wanted to have a function that returns a copy of the object it receives, to use it somewhere else. This is what I wrote: public static PObject Instantiate(PObject obj) { PObject other = obj; ...
Tiago Barros's user avatar
311 votes
21 answers
507k views

How can I clone an ArrayList and also clone its items in Java? For example I have: ArrayList<Dog> dogs = getDogs(); ArrayList<Dog> clonedList = ....something to do with dogs.... And I ...
palig's user avatar
  • 7,681
200 votes
11 answers
171k views

I'm wondering if there is a recommended way of doing deep clone/copy of instance in java. I have 3 solutions in mind, but I can have miss some, and I'd like to have your opinion edit: include Bohzo ...
Guillaume's user avatar
  • 5,564
98 votes
13 answers
301k views

I have some data structures, and I would like to use one as a temporary, and another as not temporary. ArrayList<Object> myObject = new ArrayList<Object>(); ArrayList<Object> ...
CQM's user avatar
  • 44.6k
80 votes
8 answers
95k views

Is there any utility for deep cloning for java collections: Arrays Lists Maps NOTE: prefer some solution without usage of serialization, but with use of Object.clone() method. I can be sure that my ...
Juraj's user avatar
  • 2,051
24 votes
7 answers
15k views

The error I get from the compiler is "The left hand side of an assignment must be a variable". My use case is deep copying, but is not really relevant. In C++, one can assign to *this. The ...
kostja's user avatar
  • 61.8k
5 votes
7 answers
12k views

How can I deep copy an irregularly shaped 2D array in Java? Ie. int[][] nums = {{5}, {9,4}, {1,7,8}, {8,3,2,10}} I'm unable to use Arrays....
Sean McDaid's user avatar

15 30 50 per page
1
2 3 4 5 6