简体   繁体   English

Java中的对象引用

[英]Object References In Java

Object references in java really confuse me. Java中的对象引用确实使我感到困惑。 And when I started to study linkedlist, I realized that I know verry little about the subject. 当我开始学习链表时,我意识到我对这个主题的了解很少。 My question is 我的问题是

Node node = list; 

Let's say list is the first node in my linkedlist and I know that this operation means that node refers to the same object that the list refers to. 假设list是我的链表中的第一个节点,并且我知道此操作意味着该节点引用该列表所引用的同一对象。 I mean, if I make changes to the node, consequently to the object that it refers to, list will be also affected by these changes. 我的意思是,如果我对节点进行更改,从而对它所引用的对象进行更改,列表也会受到这些更改的影响。 However, what I don't understand that do node.next and list.next also refer to the same object. 但是,我不了解的是node.next和list.next也是指同一对象。 I mean if I wrote the linkedlist like 我的意思是,如果我像这样写链表

Node node = list;
node = node.next;

Does that mean I am also changing list with the list.next? 这是否意味着我也在使用list.next更改列表? Or if I write (node.next.name = "B"), do I also change the name of the list.next element?. 或者,如果我写(node.next.name =“ B”),是否还要更改list.next元素的名称? I know the questions seems silly but I really don't know anything about references. 我知道问题似乎很愚蠢,但我对参考文献一无所知。 And I also researched the subject a lot. 而且我也对此进行了很多研究。 But what I found online so far, they didn't help me much. 但是到目前为止,我在网上发现的内容对他们没有多大帮助。 If someone explains it to me in a clear and comprehensible way, I would be very appreciated. 如果有人以清晰易懂的方式向我解释,我将不胜感激。

You know a reference means that in the memory, they have the same "reference" to the data in memory. 您知道引用是指在内存中,它们对内存中的数据具有相同的“引用”。

So it is as simple as node and list are the same object. 因此,就像nodelist是同一个对象一样简单。

If you consider that the data is stored in the memory (RAM). 如果您认为数据存储在内存(RAM)中。 Consider the memory as a closet full of drawers, 将内存视为满是抽屉的壁橱,

if the data of node are in drawer number 2. "by Reference" means that node = list means that list also refers to drawer number 2. 如果node的数据在2号抽屉中,则“按引用”表示node = list表示list也指2号抽屉。

Now whatever you change in the data, you're changing the values of drawer number 2. So you're changing both node and list 现在,无论您在数据中进行什么更改,都在更改抽屉编号2的值。因此,您同时更改了nodelist

Node node is a variable that holds a reference. Node node是一个保存引用的变量。 The "reference" is another term for a "pointer" -- the memory address of an object. “引用”是“指针”的另一个术语,即对象的内存地址。

When you assign a value to node you're storing the address of an object in that location. 当您为node分配值时,您将在该位置存储对象的地址。 The same address can be stored other places, allowing the object to be accessed from different locations in the code. 相同的地址可以存储在其他位置,从而可以从代码中的不同位置访问对象。

If you have a linked-list Node object, it presumably has fields for "next" and "name". 如果您有一个链表节点对象,则它大概具有“下一个”和“名称”的字段。 The name field in this case is presumably a reference to an object (eg, a String) that is the "value" of the node (or at least part of the node's "value). The "next" field is pretty much guaranteed to typed to hold a Node reference (though the value may be null for end-of-list). 在这种情况下,名称字段大概是对对象(例如,字符串)的引用,该对象是节点的“值”(或节点的“值”的至少一部分)。键入以保存Node引用(尽管对于列表结尾,该值可以为null)。

So your local node variable may "point" to a Node object that contains a next field that "points" to yet another Node object that contains a "next" field that points to yet another Node object, ad infinitum. 因此,您的本地node变量可能会“指向”一个节点对象,该对象包含一个next字段,该字段“指向”另一个节点对象,该对象又包含一个“下一个”字段,该字段指向另一个节点对象,这是无限的。

(One important thing to understand is that, just because you have a Node object with a "next" field that points somewhere does not mean that every Node object's "next" field points to that same somewhere. The name of the field is not the identity of the object.) (要理解的一件事很重要,就是仅仅因为您有一个Node对象,该对象的“ next”字段指向某处并不意味着每个 Node对象的“ next”字段都指向同一位置。该字段的名称不是对象的身份。)

When you do something like node.next.name = "B"; 当您执行类似node.next.name = "B"; that can be decomposed into: 可以分解为:

Node temp = node.next;
temp.name = "B";

The "name" field in the Node that you would address with node.next is changed. 您将使用node.next寻址的Node中的“名称”字段已更改。 node gives you the address of the closest Node, and node.next then gives you the address of the node next to the closest Node. node为您提供最近节点的地址,然后node.next为您提供最近节点旁边的节点的地址。

This sequence (from your original post): 此序列(摘自您的原始帖子):

Node node = list;
node = node.next;

is something you might see in code that is beginning to traverse the list. 您可能会在开始遍历列表的代码中看到一些东西。 The first line initializes node from the "list head" pointer named list . 第一行从名为list的“列表头”指针初始化node Then the second line "advances" node to the second Node in the list. 然后,第二行将节点“前进”到列表中的第二个节点。 So that after this statement node is addressing the second Node of the list. 这样,在此语句之后, node将寻址列表的第二个节点。

You asked: 您询问:

Does that mean I am also changing list with the list.next? 这是否意味着我也在使用list.next更改列表?

No, you are just reassigning the object variable (object reference) node to point at a different node in the list. 不,您只是在重新分配对象变量(对象引用) node以指向列表中的其他节点。

if I write (node.next.name = "B"), do I also change the name of the list.next element? 如果我写(node.next.name =“ B”),是否还要更改list.next元素的名称?

When node is assigned to list then the statement you wrote is true. node分配给list ,您编写的语句为true。 After node = node.next; node = node.next; the behavior will modify a different node. 该行为将修改其他节点。

It helps if you keep in mind that any NODE or subset of NODES in your list, can be thought of as a list in its own right. 如果您牢记列表中的任何NODE或NODES子集,可以认为本身就是一个列表。

Hope that helps. 希望能有所帮助。

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

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