简体   繁体   English

List和LinkedList有什么区别?

[英]What's the difference between a List and a LinkedList?

What is the difference ? 有什么区别 ?

List list = new List(); 列表列表=新的List(); LinkedList Llist; LinkedList Llist;

list.add(anyString); list.add(anyString); for example and Llist.AddFirst(anyString); 例如和Llist.AddFirst(anyString);

In which language? 用哪种语言? In Java (and probably also the language you use) List is just an interface for various types of Lists. 在Java中(可能还有您使用的语言),List只是各种类型的List的接口。 You can have single linked lists, double linked lists, circular lists etc. 您可以有单链表,双链表,循环表等。

In Java: 在Java中:

List is an interface, it does not provide a concrete implementation but only the definition of which operations are possible for classes implementing this interface. List是一个接口,它没有提供具体的实现,而只是定义了实现该接口的类可以进行哪些操作。 Ie each implementation of List must provide a size() method returning the number of elements in the list. List每个实现都必须提供一个size()方法,该方法返回列表中的元素数量。

LinkedList is a concrete implementation. LinkedList是一个具体的实现。 It supports all the methods of List (probably some more, specific to its implementation). 它支持List所有方法(可能还有更多特定于其实现的方法)。 As the name suggests, it is implementing the interface using the linked list approach . 顾名思义,它正在使用链表方法实现接口。

A different List implementation would be ArrayList , which is internally using an array to implement the interface. List另一个不同实现是ArrayList ,它在内部使用数组来实现接口。

In C#: 在C#中:

In C# the naming is different. 在C#中,命名是不同的。 List is a concrete implementation of IList . ListIList的具体实现。 Therefore IList is the interface, and List is the ArrayList implementation. 因此, IList是接口, ListArrayList实现。 There is also a LinkedList in C#. C#中也有一个LinkedList

First of all, you need to understand Interface and Implementation class in Java . 首先,您需要了解Java中的 Interface和Implementation类。 List is interface and LinkedList is concrete class implementing List interface. List是接口, LinkedList是实现List接口的具体类。

new keyword and constructor symbol () is used to instantiate (create object) for concrete classes, but cannot be used for Interface new关键字和构造函数symbol ()用于为具体类实例化(创建对象),但不能用于Interface

List list = new List();       //cannot compile as List is Interface
List list = new ArrayList();  //Valid and can compile
List list = new LinkedList(); //Valid and can compile

Before you go deep into java.util.Collection package, you should learn basic Java well. 在深入研究java.util.Collection包之前,您应该学习基本的Java。

Assuming Java.in java there are many libraries that we can work from each has its own objects so List belong to java.awt library while LinkedList belongs to java.util . 假设Java中有Java.java,我们可以使用的许多库都有各自的对象,因此List属于java.awt库,而LinkedList属于java.util

about add and addFirst() I think the difference will be like this list.add(A) list.add(B) list.addFirst(C) if you printed the list you will find the order is CAB hope it helped 关于addaddFirst()我认为区别将类似于此list.add(A)list.add(B)list.addFirst(C)如果您打印列表,您会发现订单是CAB希望对您有所帮助

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

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