Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in LinkedList and ArrayList implementation? [duplicate]

Tags:

java

Possible Duplicate:
When to use LinkedList<> over ArrayList<>?

I saw the API for the ArrayList and LinkedList and it seems to be same. Apart from their performance difference is there any difference in terms of adding, deleting and iterating the List.

List arrList = new ArrayList();

List linList = new LinkedList();

The List arrList or linList reference is actually implementing the corresponding class. What does this actually mean?

like image 729
theJava Avatar asked Sep 05 '11 02:09

theJava


People also ask

Does LinkedList allow duplicates?

4) ArrayList and LinkedList also allow duplicates and null, unlike any other List implementation e.g. Vector.

Which is better for insertion ArrayList or LinkedList?

LinkedList is faster being node based as not much bit shifting required. ArrayList implements only List. LinkedList implements List as well as Queue.

What is the main difference between ArrayList and LinkedList?

1) ArrayList internally uses a dynamic array to store the elements. LinkedList internally uses a doubly linked list to store the elements. 2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory.

What are the basic difference between using ArrayList and LinkedList as a high volume data insertion deletion and updation activities?

Elements from both LinkedList and ArrayList can be accessed randomly; however, ArrayList's time complexity is O(1), and LinkedList is O(n). Capacity and Fetching of elements: Initial capacity for Array list is ten which can be changed while in LinkedList there is no initial capacity.


2 Answers

As far as your first question goes: their performance and their memory usage the only differences that will matter to you (the third one, their actual implementation details, aren't your concern.) LinkedLists use more memory, and getting, say, the 22nd element by walking through the list from the head is very slow; but they're terrific as far as adding and removing elements in the middle of the list. ArrayLists use less memory, and getting the 22nd element is very fast -- but inserting or removing an element in the middle takes time proportional to the size of the list.

As far as your second question goes: the statement that the reference is "actually implementing the list" is just wrong, so I don't really know how to answer it. The reference variable refers to an object that implements the List interface; both of these two classes implement that interface, so a reference of type List can refer to objects of either class.

like image 169
Ernest Friedman-Hill Avatar answered Oct 20 '22 16:10

Ernest Friedman-Hill


I am not 100% sure what you mean when you ask "What does this actually mean?", but here is a guess.

Consider code like this:

interface Interface
{
   void foo();
}

class Implementation
    implements Interface
{
    public void foo() { }
    public void bar() { }
}

public class Main
{
    public static void main(final String[] argv)
    {
        Interface a;
        Implementation b;

        a = new Implementation();
        b = a;

        a.foo();
        b.foo();
        a.bar(); <-  won't compile
        b.bar();
    }
}

Interface a; and Implementation b; both point at the same object, but only the reference to "b" has access to the "bar" method.

So, in your example, any methods that are in the List interface are accessible to both arrList and linList, but any methods that they provide in addition to the List interface wont be callable without a cast. You can (and should in most cases) treat ArrayList and LinkedList as a List.

For the specifics of inserting/adding/deleting from the different lists, you generally should not care. Both behave the same way from the point of view of the end result (eg. the same sequence of method calls with the same data will result in the same result, just the internal layout will be different).

like image 28
TofuBeer Avatar answered Oct 20 '22 17:10

TofuBeer