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?
4) ArrayList and LinkedList also allow duplicates and null, unlike any other List implementation e.g. Vector.
LinkedList is faster being node based as not much bit shifting required. ArrayList implements only List. LinkedList implements List as well as Queue.
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.
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.
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.) LinkedList
s 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. ArrayList
s 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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With