简体   繁体   English

Java链接列表,创建无名节点

[英]java- linked lists, creating nameless nodes

my question is perhaps too simple: maybe that's the reason i didn't manage to find a written answer. 我的问题也许太简单了:也许这就是我没有找到书面答案的原因。 i am trying to understand the way linked lists are built, and there is one thing i can't understand: usually, when i create an object of some kind of class, i need to give it a name, like that: 我试图了解链接列表的构建方式,但有一件我无法理解的事情:通常,当我创建某种类的对象时,需要给它起一个名字,例如:

 cat kitten =new cat();

at the same time when i do that, the computer gives it an address, something like dfe@fggv3444. 在执行此操作的同时,计算机会为其提供一个地址,例如dfe @ fggv3444。 when i want to use the created object in some method, i address it by the name i gave it: in our case :"cat". 当我想以某种方法使用创建的对象时,我用我给它的名称来解决它:在我们的例子中是“ cat”。 the logic is that if i name 2 objects in the same name "cat", the computer wouldn't know which object to access- or perhaps, both. 逻辑是,如果我用相同的名称“ cat”命名2个对象,则计算机将不知道要访问哪个对象,或者可能都不知道。 and now the question: when we use a linked list, the number of used nodes changes a lot throughout the program. 现在的问题是:当我们使用链表时,整个程序中使用的节点数变化很大。 so how this objects of node class type are getting their names? 那么节点类类型的对象如何获得其名称? obviously they must have a name, but i am not there to give each node its name... 显然他们必须有一个名字,但是我不在那里给每个节点起名字...

how it works?! 这个怎么运作?!

thanks in advance. 提前致谢。 i am sure that something in this question must be silly, but can't figure out what. 我确定这个问题中的某些内容一定很愚蠢,但无法弄清楚是什么。

Each cat name is essentially (as @Turing85 pointed out in the comments) just a memory address to a cat instance in a human-readable format. 每个cat名称本质上(如注释中的@ Turing85所指出的)只是人类可读格式的猫实例的内存地址。

A linked list at its core is a set of nodes, each with a value and the next node ( next being the variable that connects the nodes to form the list). 链表的核心是一组节点,每个节点都有一个valuenext节点( next是将节点连接起来以形成列表的变量)。 Each cat would be referred to as simply node.value , where value is the "name" pointing to the address of the node's cat. 每只猫将简称为node.value ,其中value是指向节点猫的地址的“名称”。 Each node's next variable points to the next node, which has its own value . 每个节点的next变量指向具有自己的value的下一个节点。

You can't name two objects the same thing within the same scope - you can have cat1 and cat2 both of type cat , and each points to the memory address of a different cat . 您不能在同一范围内为相同的对象命名-您可以使cat1cat2都属于cat类型,并且每个都指向不同cat的内存地址。 The reason linked lists can do this is because each node has its own scope, that no other node sees. 链表之所以能够做到这一点,是因为每个节点都有自己的作用域,而其他节点看不到。 Hence all nodes can have a next and a value , referred to as node.next and node.value . 因此,所有节点都可以有一个next和一个value ,称为node.nextnode.value

      node1        +--------> node2        +--------> node3
+--------------+   |    +--------------+   |    +--------------+
| value = cat1*|   |    | value = cat2 |   |    | value = cat3 |
| next = node2-|---+    | next = node3-|---+    | next = null  |
+--------------+        +--------------+        +--------------+

*---> cat1
+--------------+
| name = "tom" |
| col = "grey" |  ...and likewise for the other nodes
| ............ |

Java has a built in class for linked lists java.util.LinkedList When you instantiate this object ie. Java实例化此对象时,Java有一个用于链接列表的内置类java.util.LinkedList

LinkedList<Node> list = new LinkedList<Node>();

You are creating a reference , or in your case name "list" to the object (your linked list) 您正在创建引用 ,或者在您的情况下, 名称为对象(您的链接列表)的“列表”

The entire list has a name "list" which can act as a starting point for your traversal. 整个列表都有一个名称“列表”,可以作为遍历的起点。 There are no "names" for each node in the linked list. 链表中的每个节点都没有“名称”。 In java, you can traverse the list somewhat similar to how you would an array; 在Java中,您可以遍历列表,有点类似于数组。 through an index. 通过索引。

list.get(someIndex)

This method basically traverses the list until someIndex, and returns the node there. 此方法基本上遍历列表,直到someIndex,然后返回那里的节点。 You can manually set a name for that node by doing; 您可以通过手动为该节点设置名称。

Node node = list.get(someIndex)

But in general, they do not have a "name", instead you can access them through their index, or many other methods. 但是通常,它们没有“名称”,而是可以通过它们的索引或许多其他方法来访问它们。

Check https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html for more details on the built in class 检查https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html,以获取有关内置类的更多详细信息

In linked list, each element is represented by nodes. 在链表中,每个元素都由节点表示。 Those nodes hold onto the other elements by their addresses in memory. 这些节点通过它们在内存中的地址保留其他元素。 This could be pseudo-name, but in reality it's just a way to represent where it is in memory. 这可能是伪名称,但实际上,这只是表示其在内存中的位置的一种方式。

What might make it confusing is that you do not define the object directly like you would do in this code. 可能使您感到困惑的是,您没有像在此代码中那样直接定义对象。

Animal cow = new Animal();

In this instance we have assigned the address of new object's data in the variable cow . 在这种情况下,我们在变量cow分配了新对象数据的地址。 This allows us to easily access it. 这使我们可以轻松访问它。

In contrast to that, linked list node objects hold the address of the next node, but just like any other object, they are a contained as just that node's attribute. 与此相反,链表节点对象保留下一个节点的地址,但与其他任何对象一样,它们只是该节点的属性而被包含。

You cannot give the same name to any two variables in Java, within the same scope of visibility (while both variables still exist, and not trashed by the Java Garbage collector. more info : http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html ), otherwise the compiler wouldn't know which value or object you're referring to. 您不能在相同的可见性范围内为Java中的任何两个变量赋予相同的名称(而这两个变量仍然存在,并且不会被Java垃圾收集器丢弃。)更多信息: http : //www.oracle.com/webfolder/ technetwork / tutorials / obe / java / gc0​​1 / index.html ),否则编译器将不知道您所指的是哪个值或对象。

To answer the second part of your question, an object exists as long as it has a reference (way to access the object). 要回答问题的第二部分,只要对象具有引用 (访问该对象的方式)就存在。 You are right to understand that obviously, an object added to the linked list will have to be accessed. 您应该理解,很显然,必须访问添加到链接列表的对象。 Suppose the first node is named head, and you have to find a reference to the third node in a linked list. 假设第一个节点名为head,并且您必须在链接列表中找到对第三个节点的引用。 head.next.next will be a reference to the third node in the linked list, it does not necessarily have to have a name like 'head', but will exist as long as it has a reference. head.next.next将是对链表中第三个节点的引用,它不一定必须具有“ head”之类的名称,但只要有引用就将存在。

Hope this answers your question! 希望这能回答您的问题!

A variable name is only for readability. 变量名仅出于可读性。 Java compiler translates source code into "machine-readable" code, which contains no variable name. Java编译器将源代码转换为“机器可读”代码,其中不包含变量名。 Values are stored in memory with specific addresses, such as 0x00000000 in hexdecimal. 值存储在具有特定地址的内存中,例如十六进制的0x00000000

Each node contains a value and the memory address of the next node. 每个节点包含一个值和下一个节点的内存地址。 When you call cat.next(); 调用cat.next(); , your computer gets the address of the next node instead of a variable name, and it knows where this address is. ,您的计算机将获取下一个节点的地址而不是变量名,并且它知道该地址在哪里。

Note: 注意:

  1. Values may also be stored in register, cache, or disk. 值也可以存储在寄存器,高速缓存或磁盘中。 They also refer to addresses for data. 他们还引用数据地址。
  2. Java LinkedList is actually doubly-linked, where each node contains address of its previous node as well. Java LinkedList实际上是双重链接的,其中每个节点也包含其先前节点的地址。 We use singly-linked list here for simplification. 为了简化起见,我们在这里使用单链表。

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

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