Why is the Object class's clone() method giving a deep copy of object?

As per the JAVA documentation, the super.clone() when called returns a shallow copy of the object. In the code below I have two objects name and id ; and one primitive variable num . When the super.clone() method is called on the first object, it seems to be creating a deep copy of the objects(name and id) in addition to an expected copy of only num. After cloning the object obj, I have chang

为什么Object类的clone()方法会提供对象的深层副本?

根据JAVA文档,super.clone()被调用时会返回该对象的浅表副本。 在下面的代码中,我有两个对象名称和ID ; 和一个基本变量num 。 当在第一个对象上调用super.clone()方法时,除了仅有num的预期副本外,它似乎还创建了对象的深层副本(名称和ID)。 克隆对象obj后,我更改了它的名称和id字段。 如果正在创建浅拷贝,这些更改应该反映在克隆的对象中。 我对吗? public class Cloning implements Cloneable { String name

Is clone() in java shallow copy?

Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). I read this from wikipedia. I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a

java中的clone()是浅拷贝吗?

java中的clone()是一个浅拷贝吗? 最后,这将获得Object(最上面的类)的clone()方法,该方法创建与对象相同的类的新实例,并将所有字段复制到新实例(“浅拷贝”)。 我从维基百科读到这个。 我不明白为什么它是一个浅拷贝。 clone()将创建一个包含所有字段的新实例。 这只是一个深刻的副本? 困惑。 我需要一些解释。 默认的Object.clone()确实是一个浅拷贝。 但是,它的目的是抛出一个CloneNotSupportedExceptio

Original object is also changed when values of cloned object are changed

I am trying to use clone but the original object is also changed when values of cloned object are changed. As you can see KalaGameState does not use any objects so a shallow copy should work. /** * This class represents the current state of a Kala game, including * which player's turn it is along with the state of the board; i.e. the * numbers of stones in each side pit, an

克隆对象的值更改时,原始对象也会更改

我正在尝试使用克隆,但当克隆的对象的值发生更改时,原始对象也会发生更改。 正如你可以看到KalaGameState不使用任何对象,所以浅拷贝应该工作。 /** * This class represents the current state of a Kala game, including * which player's turn it is along with the state of the board; i.e. the * numbers of stones in each side pit, and each player's 'kala'). */ public class Kala

Java: recommended solution for deep cloning/copying an instance

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 propositon and refine question: it's more about deep cloning than shallow cloning. Do it yourself: code the clone by hand properties after properties and check that mutable instance

Java:深层克隆/复制实例的推荐解决方案

我想知道是否有推荐的方法在java中做深层克隆/副本实例。 我有三个解决方案,但我可能会错过一些,我想听听你的意见 编辑:包括Bohzo提议和改进问题:关于深克隆更多的是浅层克隆。 自己做: 在属性之后用手属性编码克隆并检查可变实例是否被克隆。 亲们: - 控制将要执行的操作 - 快速执行 缺点: - 繁琐的编写和维护 - 容易出错(复制/粘贴失败,缺少属性,重新分配可变属性) 使用反射: 使用自

Storing Names in ArrayList and Using it to Login

I have a task to do which involves asking the user to input their last name and giving the user an account number to login to the program. I have listed the steps below which might make more sense. 1) User creates an account 2) User enters their last name (Stores into the arraylist) 3) User is given an account number (Stores into the arraylist) 4) User can then login using their last nam

在ArrayList中存储名称并使用它登录

我有一个任务要做,其中包括要求用户输入他们的姓,并给用户一个账号登录到程序。 我列出了下面可能更有意义的步骤。 1)用户创建一个帐户 2)用户输入他们的姓氏(存储到数组列表中) 3)给用户一个帐号(存入数组列表) 4)然后用户可以使用他们的姓氏和帐号登录(检查姓氏和帐号的arraylist,如果匹配,则登录消息,如果没有错误消息) 用户输入他们的姓氏,并给他们一个帐号,然后他们使用它登录以存款,取款和查

Having trouble deep cloning an object in a linked list

Hello guys I'm having trouble with cloning. I am trying to clone an object within a list. I succeeded in cloning my list and the node in the list. But i am having trouble cloning an object that is stored in the node. Below is my sample code: I succeeded in cloning the class Node and class B. But not succeeding in cloning class A from class B as i am trying to modify class A with the clone

无法深入克隆链接列表中的对象

你好,我在克隆方面遇到了麻烦。 我正在尝试在列表中克隆一个对象。 我成功地克隆了我的列表和列表中的节点。 但我无法克隆存储在节点中的对象。 以下是我的示例代码:我成功克隆了类节点和类B,但未成功克隆类B中的类A,因为我试图用克隆LinkedList修改类A,并通过打印出原始文件来验证其深层副本内容和克隆的内容,并且能够在不修改原始内容的情况下修改克隆的内容。 基本上我的程序是从一个文件中读取并存储一个国家的统

How to clone ArrayList and also clone its contents?

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 would expect that objects in clonedList are not the same as in dogs list. You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go. publi

如何克隆ArrayList并克隆其内容?

我如何克隆一个ArrayList并且在Java中克隆它的项目? 例如,我有: ArrayList<Dog> dogs = getDogs(); ArrayList<Dog> clonedList = ....something to do with dogs.... 我期望clonedList中的对象与狗列表中的对象不同。 您需要对这些项目进行迭代,并逐个克隆它们,随时将克隆放入结果数组中。 public static List<Dog> cloneList(List<Dog> list) { List<Dog> clone = new ArrayList&

BufferedImage : extract subimage with same data

I would like to extract a rectangle of a BufferedImage. Javadoc propose getSubImage(x,y,w,h) and getData(rectangle). getData is cool but I don't want only the raster. I want the subimage as a BufferedImage object but I also need a modified version of it data array but the javadoc says public BufferedImage getSubimage(int x,int y,int w,int h) : Returns a subimage defined by a specified

BufferedImage:用相同的数据提取子图像

我想提取一个BufferedImage的矩形。 Javadoc提议getSubImage(x,y,w,h)和getData(矩形)。 getData很酷,但我不想只有光栅。 我想把子图像作为一个BufferedImage对象,但我也需要它的数据数组的修改版本,但javadoc说 public BufferedImage getSubimage(int x,int y,int w,int h):返回由指定的矩形区域定义的子图像。 返回的BufferedImage与原始图像共享相同的数据数组 。 问:我怎样才能提取一个收缩数据

How do I copy an object in Java?

Consider the code below: DummyBean dum = new DummyBean(); dum.setDummy("foo"); System.out.println(dum.getDummy()); // prints 'foo' DummyBean dumtwo = dum; System.out.println(dumtwo.getDummy()); // prints 'foo' dum.setDummy("bar"); System.out.println(dumtwo.getDummy()); // prints 'bar' but it should print 'foo' So, I want to copy the dum to dumtwo and change dum without affecting the dumtwo .

如何复制Java中的对象?

考虑下面的代码: DummyBean dum = new DummyBean(); dum.setDummy("foo"); System.out.println(dum.getDummy()); // prints 'foo' DummyBean dumtwo = dum; System.out.println(dumtwo.getDummy()); // prints 'foo' dum.setDummy("bar"); System.out.println(dumtwo.getDummy()); // prints 'bar' but it should print 'foo' 所以,我想将dum复制到dumtwo并在不影响dumtwo情况下更改dum 。 但上面的代码没有这样做。 当

Java PriorityQueue with custom Comparator

I am using a PriorityQueue and my own Comparator but somehow the end results are not always good. I should sort by grade average, than name, than id.no. At the end it should return the names left in the queue ordered. The remaining names are fine but their order is not. Input (name, grade avg, id.no): add John 3,75 50 add Mark 3,8 24 add Shafaet 3,7 35 poll poll add Samiha 3,85 36 poll add A

具有自定义比较器的Java PriorityQueue

我使用的是PriorityQueue和我自己的比较器,但不知怎的,最终结果并不总是好的。 我应该比平均年级,比名称,id.no. 最后,它应该返回排在队列中的名字。 其余的名字都很好,但是他们的顺序不是。 输入(名称,等级avg,id.no): add John 3,75 50 add Mark 3,8 24 add Shafaet 3,7 35 poll poll add Samiha 3,85 36 poll add Ashley 3,9 42 add Maria 3,6 46 add Anik 3,95 49 add Dan 3,95 50 poll 预期产出: Dan Ashl