简体   繁体   English

数组分配操作问题

[英]Array assignment operation question

If I have a piece of code like this: 如果我有一段这样的代码:

MyClass[] objArray = new MyClass[7];
//assign values to objArray
//do something here
//sometime later
MyClass newObj = new MyClass();
objArray[3] = newObj;

The last statement above will do the following: 上面的最后一条语句将执行以下操作:

  • copy all the contents of the newObj to the space referred to by objArray[3]. newObj所有内容复制到objArray [3]引用的空间。

Questions 问题

  1. Am I right? 我对吗?

  2. Shallow copy or deep copy? 浅拷贝还是深拷贝?

  3. If it is shallow copy, how can I make the deep copy possible? 如果是浅拷贝,如何使深拷贝成为可能?

     objArray[3] = newObj; 
  4. Does this rule applies to other Java container types, such as Queue, List, ...? 此规则是否适用于其他Java容器类型,例如Queue,List等?

Well, it will copy "all the contents of the newObj " into objArray[3] ... but the contents (or value) of the newObj variable is just a reference to the object. 那么,它 “的全部的内容复制newObj ”到objArray[3] ... 内容(或值) newObj变量只是对对象的引用。 In other words, consider: 换句话说,请考虑:

objArray[3] = newObj;
newObj.setFoo("hello");

System.out.println(objArray[3].getFoo()); // prints "hello"

(assuming a simple property, of course). (当然,假设一个简单的属性)。

Basically, the value of a variable (including array elements) is never an object. 基本上,变量(包括数组元素)的值永远不是对象。 It's always a reference or a primitive value. 始终是引用或原始值。 It's always passed or copied by value. 总是按值传递或复制。

Answer 1&2: No. Only a reference to the object is copied. 答案1&2:否。仅复制对对象的引用。 newObj and objArray[3] will afterwards refer to the same object instance. newObjobjArray[3]将引用同一对象实例。

Answer 3: If you want a copy, you have to implement it yourself. 答案3:如果要复制,则必须自己实施。 You could implement a copy constructor or Clonable , or for a simple deep copy, serialize and deserialize the object, but that requires it and all objects it consists of to be Serializable 您可以实现一个副本构造函数或Clonable ,或者对于一个简单的深层副本,对对象进行序列化和反序列化,但这需要它以及它所组成的所有对象都可以Serializable

Answer 4: It's exactly the same for all Java Objects: the reside on the heap, and the code works only with references to the objects. 答案4:对于所有Java对象,它都是完全相同的:驻留在堆上,并且代码仅适用于对对象的引用。 Container types usually implement a copy constructor that does a shallow copy. 容器类型通常实现执行浅表复制的复制构造函数。 There is no deep copy functionality that is automatically available for all classes. 没有适用于所有类的深层复制功能。

  1. No, it does not copy the content. 不,它不会复制内容。 It just creates another reference. 它只是创建另一个参考。
  2. Shallow. 浅。 (See 1) (参见1)
  3. You cannot use that statement if you want a deep copy. 如果要深层复制,则不能使用该语句。 You have to override the clone-method of java.lang.Object in your Class MyClass and use that or create copy constructor. 您必须在Class MyClass重写java.lang.Object的克隆方法,然后使用它或创建副本构造函数。
  4. It applies to all data types that are not primitives like int or double; 它适用于所有不是原语的数据类型,例如int或double;

copy all the contents of the newObj to the space referred by objArray[3]. 将newObj的所有内容复制到objArray [3]引用的空间。

Nope, it will store a reference to [the Object referenced by: thanks Jon Skeet ] newObj at objArray[3]. 不会,它将在objArray [3]中存储对[Object by by: Jon Skeet引用] newObj的引用。 The original object is not changed or copied in any way, just the reference to it. 原始对象不会以任何方式更改或复制,只是对其的引用。

no copy at all. 完全没有副本。 The reference to the object is set in the array. 对对象的引用在数组中设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM