对象的ArrayList引用 [英] ArrayList references to objects
问题描述
我意味着什么有关的ArrayList持有对象的引用有点困惑。有人可以给我如何在这个code所示的例子吗?此外,可以ArrayList中有一个是对自身的引用的元素?谢谢!
I'm a little confused on what it means about ArrayLists holding references to objects. Can someone give me an example on how this is shown in code? Also, can arraylist have an element that is a reference to itself? Thanks!
推荐答案
您必须启动变量,值和对象之间做区分。
You have to start making distinctions between variables, values, and objects.
变量是容器。它们包含的值。
Variables are containers. They contain values.
值可以是两种类型,原始和参考的。参考值是对象指针。
Values can be of two types, primitive and reference. Reference values are pointers to objects.
对象是数据结构,其中也有行为。
Objects are data structures which also have behavior.
因此,在
Object var = new Object();
VAR
是一个变量。 新的对象()
是一个新的实例创建前pression计算结果为类型的值对象
,即值类型为对象
的对象的引用。然后,该值被分配给 VAR
。
var
is a variable. new Object()
is a new instance creation expression that evaluates to a value of type Object
, that value is a reference to an object of type Object
. That value is then assigned to var
.
您就可以使用 VAR
来调用一个方法
You can then use var
to invoke a method
var.toString();
的运行时环境将评估 VAR
,其产生的参考值,检索引用的对象,并调用方法。您可以通过执行重新分配存储在 VAR
值
The runtime environment will evaluate var
, which produces a reference value, retrieve the referenced object, and invoke the method. You can reassign the value stored in var
by doing
var = someOtherValue;
现在 VAR
将举行新的价值。
这是的ArrayList
在后台使用的数组。数组是一种特殊的对象,其中的元素(可以认为的字段)本身的变量。
An ArrayList
uses an array behind the scenes. An array is a special object where its elements (which you can think of as fields) are themselves variables.
所以
Object[] arr = new Object[10];
是类型的数组对象
,其中包含10个变量。这些变量包含键入对象
的值。再次,这些值的引用。您可以对其进行评估,也可以重新分配他们。
is an array of type Object
which contains ten variables. These variables contain values of type Object
. Again, these values are references. You can evaluate them or you can reassign them.
arr[3].toString();
arr[7] = "maybe a string";
arr[9] = arr[9]; // possible, but makes no sense
这篇关于对象的ArrayList引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!