I wrote a self-answered question that was labeled as duplicate. I don't agree with that, but my question is, how can I save my high-quality answer now? Duplicates are automatically deleted, aren't they? Which means my answer is going to be deleted too. It's unfortunate since I believe it's valuable for SO community members, especially beginners
-
12Duplicates are not, in general, automatically deleted. However, if your answer can be made to apply to the other "main" question, then you should just repost it there (with some editing as necessary), and delete the one you posted on the "duplicate".– Cody Gray - on strike ModJun 13 at 13:11
-
@CodyGray-onstrike since I don't believe it's a duplicate, I don't believe I can just move the answer– Sergey ZolotarevJun 13 at 13:15
-
6Well, okay. I lack sufficient subject-matter expertise when it comes to Java to have an opinion about whether or not the question is a duplicate. If you think the question is not a duplicate, then your focus should probably be on that, with the goal of getting it re-opened. Otherwise, if all you care about is your answer sticking around, then it will continue to do so. A question with an answer will not be deleted automatically (full rules for that here).– Cody Gray - on strike ModJun 13 at 13:32
-
2Questions closed (as duplicates) can still turn up in search (engine) results too. These questions can, and do, still receive views and votes . Reopening because it "adds something of value" isn't a reason to reopen because the post(s) still do provide that, even in a closed state; closing does not stop a question adding value to the site's content.– Thom AJun 13 at 13:54
-
"Duplicates are automatically deleted, aren't they?" - They are not deleted, which is the reason, asking a question that is later closed as a duplicate isn't a necessary a negative event. It only negative, if the question receives downvotes, because the duplicate was presented to the user asking the question before submitting the question. Furthermore, you could always delete the answer, and resubmit it to the original question. However, I would take the feedback you received to heart, and consider the existing answer to the duplicate already expressed your solution.– Security HoundJun 13 at 14:00
-
1stackoverflow.com/help/auto-deleted-questions– toolicJun 13 at 14:16
-
I read your argument in the question about why it shouldn't be a duplicate, and have been convinced that I am not missing anything and it is, indeed, a duplicate. Either leave your question as-is, or post your answer to the original question.– EstherJun 13 at 15:59
-
3It is not up to you to judge whether your answer is a "high-quality" answer, but up to the community, which expresses this with votes.– Rabbid76Jun 13 at 19:38
-
3Just because you are unable to understand that it is a duplicate does not make it any less of a duplicate.– JK.Jun 14 at 3:58
-
Why do you have to be so toxic? Every time I post on meta, you go and downvote my publications left and right, including the mentioned answer that is obviously a positive contribution to SO (if I came across it a few weeks ago, I would definitely find it useful). It's shameful– Sergey ZolotarevJun 14 at 12:32
-
How is the jDoodle question bad? You even have a tag for that, and I did show my efforts to find the answer myself. Whatever the reason you're do full of spite, that reason has nothing to do with me or my questions– Sergey ZolotarevJun 14 at 12:36
2 Answers
Deletion policy
Duplicates are automatically deleted, aren't they? Which means my answer is going to be deleted too.
No, the site does not work like that. I genuinely have no idea how you came to this conclusion. The only things that get automatically deleted are per roomba policy. The normal way questions get deleted (other than by the OP) is that they are closed, downvoted to -3 or below, and then voted to delete by the community.
The site software is in fact extremely conservative about keeping around possibly useful information. Duplicates, in particular, are kept because a duplicate question can act as a "signpost" for a canonical question (meaning, a question that is used as the target for many other duplicate links). This has two main benefits:
The number of questions in the system that are marked as a duplicate of a canonical, is useful information that helps us know how important that canonical is. It's the basis for sorting by "frequent".
If the title is a good description for the problem, while differing significantly from the canonical, it can help people find the canonical. For logged-out users, the site software will automatically follow the link when a question has been closed as a duplicate of exactly one other question, so this helps people using external search engines as well as people looking around within the site.
The question is very clearly a duplicate
Before we can consider what to do with the answer, it's necessary to understand how duplicates are judged on Stack Overflow. (Ironically, everyone who protests "my question is not a duplicate!" seems to require a custom explanation.)
In your problem setup, there is a class called Person
. In some disembodied code (but because this is Java, for it to be runnable it would have to be inside a method of some class somewhere), an array of Person
s is created (Person[] arr
), and then there is a blank where the code should be to deep-copy the array, and then a request that modifying a Person
in the copied array should not result in an observable change to the original array. The reason why such a change would be observed, of course, is because a naive copy would be shallow - the arrays would contain the same actual Person
instances. This cannot be fixed "automatically"; the code needs to use some explicit mechanism that "clones" Person
instances (creates new instances that are equal to an original instance, but which have a separate identity).
In the problem setup of the current duplicate, there is a class called Position
. Then, inside another class PositionList
, there is a field which is an array of Position
s (private Position[] data
), and then there is a failed attempt at code to deep-copy the array, and then an implied complaint that modifying a Person
in the copied array should not result in an observable change to the original array. The reason why such a change would be observed, of course, is because the naive copy actually shown is shallow - the copied array contains the same actual Position
instances. This cannot be fixed "automatically"; the code needs to use some explicit mechanism that "clones" Position
instances (creates new instances that are equal to an original instance, but which have a separate identity).
What's different between these cases?
The class stored in the array is named differently, and presumably has different contents; and you explicitly show the contents of the class in your version. However, this is not relevant; any user-defined class with mutable instances would suffice to demonstrate the same problem. The problem is not related to the implementation of that class, but to the deep-copying algorithm.
Your attempt at the question simply asks how to do it, whereas the reference duplicate shows an attempt. This is not really relevant; it is not useful to distinguish "how do I do X?" from "why doesn't the most obvious/naive attempt at X actually accomplish X?". Nobody who wrote such code and experienced a failure would be satisfied by simply knowing what is wrong with the attempt, and consequently any sensible explanation of how to do the task properly would naturally explain the issue with the non-working naive attempt (because there's no reason to put that information anywhere else).
You don't show the array being a field in some other class, and indeed don't show class or method context for the copying code at all. But none of this is relevant; the problem and solution are still unchanged - as you should reasonably know, given that you understand the problem well enough to answer the question yourself.
In other words: nothing that matters to us. This is a duplicate, clear as day.
Ideally, the existing question should be edited to improve it by making a proper MRE with a concrete description of failure (rather than "my tests don't work"). However, it's already clear and focused, and it explicitly asks (and always has asked) about "making a deep copy" (implying that OP had a basic understanding of the issue).
What do I do now?
The same thing that you would do if you had never written the Q&A, but instead are looking at the canonical Q and have your current A sitting in a text file on your computer. That is:
Check if existing answers describe these approaches to the problem. Is there an answer showing the use of
SerializationUtils.clone
? Does your answer explain anything about how to use it, that isn't covered by such an existing answer? How about the approach using afor
loop and constructor calls? What are the limits on these approaches? (You already know this:SerializationUtils.clone
requires a class thatimplements Serializable
; and the constructor approach requires a constructor that can clone an arbitrary valid object state, along with a way to infer arguments for that constructor from an arbitrary instance.)Check if the proposed answer adds useful information. Do other answers mention that the
clone
method of the array,Arrays.copyOf
,System.arraycopy
and manual copying with afor
loop and assignment are all shallow copies? (I didn't check in detail, but we can already write off the last one, because the question addresses that.)Check if it would be better to improve existing answers by editing to fix an obvious oversight. If you are convinced that you have, in your proposed answer, covered something new, then edit what you have down to the part necessary to focus on the actually new information; edit again to make it work within OP's framing; and post that answer to the canonical.
In this particular case: the accepted/top answer for the canonical already clearly describes the entire problem, mentions the copy-constructor approach along with its limitations, and mentions cloning by serialization (using ObjectOutputStream
/ObjectInputStream
to re-create the instance from an actual serialized form). It also mentions using .clone
on the instance type, and cases where it would be necessary to override the Object.clone
implementation. (As an aside: since String
s are immutable, in your case you should also have been able to deep-copy this way, without needing a Person.clone
implementation.)
To my understanding, SerializationUtils.clone
is essentially a wrapper for the ObjectOutputStream
/ObjectInputStream
approach. It might be worth editing the top answer to describe how to do this. Otherwise, I honestly don't see how your answer possibly adds any information that isn't already well covered.
-
1This comment is really superfluous, but wow. Thanks for generously taking the time to write such a detailed and informative answer.– tripleeeJun 14 at 4:13
-
1"some disembodied code (but because this is Java, for it to be runnable it would have to be inside a method of some class somewhere)" - stop trying to make this a thing. A question or answer doesn't have to contain code that is copy-pasteable and runnable without modification or boilerplate. Jun 14 at 7:24
-
1@CodeCaster true for the site rules, but less true for the quality rating. The more attractive and self-explanatory the code looks, the more it'll help towards attracting upvotes. I would not dismiss advice tailored to prevent bad experiences on the site so easily. Unfortunately it is not really framed as advice but rather just a random bit of trivia.– GimbyJun 16 at 12:16
-
@Gimby I have "clashed" with Karl on this subject more than once. They want a question or an answer to contain everything from (obvious) imports to a Program class with a Main method. In my opinion, that is noise. Someone who's searching for an answer to a specific problem already has the containing program, they're just looking for the fixed code (and some explanation of what was fixed, hopefully). Jun 16 at 12:24
Your entire Q&A is a setup to promote an approach you found, namely SerializationUtils.clone(arrayOfSerializableObjects)
.
The rest is noise about methods that don't work. Showing an approach that doesn't work is definitely useful, but not a reason it's not a duplicate. Besides, you casually mention that the class needs to implement Serializable, but you don't mention that needs to be transitive as well, i.e. for properties within your class that are of other class types
The question remains the same, however: how do deep-clone a collection of objects. Just move your answer to the duplicate.