简体   繁体   English

ArrayList与单链表是一回事吗?

[英]Is an ArrayList the same thing as a singly-linked list?

In Java, I have been asked to store integer values in a singly-linked list and then print the elements stored in the list. 在Java中,我被要求在单链表中存储整数值,然后打印存储在列表中的元素。 Here is what I came up with: 这是我想出的:

int max = 10;
List<Integer> list = new ArrayList<Integer>();

for (int num = 0; i < max; i++){
     list.add(num);
}
System.out.print(list);

I was wondering, is ArrayList the same thing as a singly-linked list? 我想知道,ArrayList和单链表一样吗? I want to make sure that I'm answering the question correctly. 我想确保我正确地回答了这个问题。 Does this make sense? 这有意义吗? Thanks! 谢谢!

No - ArrayList is not a linked list at all - it's an array list. 否 - ArrayList根本不是链表 - 它是一个数组列表。 ArrayList stores its elements in an array, whereas linked lists store them in arbitrary memory by linking objects together. ArrayList将其元素存储在数组中,而链接列表通过将对象链接在一起将它们存储在任意内存中。

LinkedList is a doubly-linked list, and I'm sure that there are various singly linked list implementations you could grab, but given that this is an assignment you'll either be marked down or fail outright if you try to hand in code using someone else's implementation. LinkedList是一个双向链接列表,我确信你可以抓取各种单链表实现,但鉴于这是一个赋值,你要么被标记为失败要么直接失败如果你试图用别人的实施。

Instead, find an article etc. which describes linked lists and try to implement one yourself. 相反,找到描述链接列表的文章等,并尝试自己实现。

Generally these are built in java by having a class SomeClass containing a forward link of type SomeClass and a value. 通常这些是在java中构建的,它包含一个SomeClass类,它包含SomeClass类型的前向链接和一个值。 You build the list by linking each SomeClass instance to the next through the forward link. 您可以通过前向链接将每个SomeClass实例链接到下一个实例来构建列表。

No ArrayList is definitely not the same as a singly linked list. 没有ArrayList绝对不是单链表。 In fact, it is not a linked list at all: it is a list that uses an array for its backing storage. 实际上,它根本不是链表 :它是一个使用数组作为其后备存储的列表。 This lets you access ArrayList in arbitrary order, as opposed to linked lists, that must be accessed sequentially. 这使您可以按任意顺序访问ArrayList ,而不是必须按顺序访问的链接列表。

Java library has a doubly-linked list, but no singly-linked one; Java库有一个双向链表,但没有单链表; you need to write it yourself. 你需要自己写。

There are several good implementations available on the internet; 互联网上有几个很好的实现; take a look at this answer on the codereview site to gain some ideas on how to implement a singly linked list of your own. 在codereview网站上查看这个答案,以获得有关如何实现您自己的单链表的一些想法。

No, ArrayList is an implementation of the List interface that uses a backing array to store the data. 不, ArrayListList接口的实现,它使用支持数组来存储数据。 It sounds like the assignment wants you to write your own singly-linked List implementation. 听起来这个任务要求你编写自己的单链接 List实现。

No, an ArrayList is backed by an array. 不, ArrayList由数组支持。 Arrays utilize contiguous storage (that is, the start of the array + offset of size of whatever is stored in the array == next element). 数组使用连续存储(即数组的开头+存储在数组== next元素中的任何内容的大小的偏移量)。 Java has a LinkedList class, however, this is a doubly linked list, meaning that it contains two references: one to the previous element, and one to the next element. Java有一个LinkedList类,但是,这是一个双向链表,这意味着它包含两个引用:一个引用前一个元素,一个引用下一个元素。

Java does not have a singly linked list as a built in data structure. Java没有单链表作为内置数据结构。 Either the question is asking you write your own implementation of a singly linked list, or is incorrect when it says to use one. 问题是要求你编写自己的单链表实现,或者说它使用一个是不正确的。

The name "Array" List states that the underlying implementation uses "arrays" for management. 名称"Array" List表示底层实现使用“数组”进行管理。 Whenever we talk about "Linked" List we are actually thinking about "Noded" lists ie every element has got a pointer to the forward node (and previous node if doubly-linked list). 每当我们谈论"Linked"列表时,我们实际上都在考虑“Noded”列表,即每个元素都有一个指向前向节点的指针(如果是双向链接列表则是前一个节点)。 At least that's what this DSA book (by Granville Barnett and Luca Del Tongo) is saying. 至少就是这本DSA书 (由Granville Barnett和Luca Del Tongo撰写)所说的。

In commercial Life your Solution is Perfect, but unfortunately ist seems that you are requested to use a Single linkes List which in Std Java does Not exist. 在商业生活中,您的解决方案是完美的,但遗憾的是,您似乎要求使用Std Java中不存在的单链接列表。 (And would Not make much sense) For Purpos of Training you have to Write your own List. (并且没有多大意义)对于训练的目的,你必须写自己的清单。

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

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