What is the difference ?
List list = new List(); LinkedList Llist;
list.add(anyString); for example and Llist.AddFirst(anyString);
What is the difference ?
List list = new List(); LinkedList Llist;
list.add(anyString); for example and 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. You can have single linked lists, double linked lists, circular lists etc.
In 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. I.e. each implementation of List
must provide a size()
method returning the number of elements in the list.
LinkedList
is a concrete implementation. It supports all the methods of List
(probably some more, specific to its implementation). 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.
In C#:
In C# the naming is different. List
is a concrete implementation of IList
. Therefore IList
is the interface, and List
is the ArrayList
implementation. There is also a LinkedList
in C#.
First of all, you need to understand Interface
and Implementation class in Java.
List
is interface and LinkedList
is concrete class implementing List
interface.
new
keyword and constructor symbol ()
is used to instantiate (create object) for concrete classes, but cannot be used for 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.
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
.
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 C A B
hope it helped