View RSS Feed

My Java Tips

LinkedList - insertion/deletion

Rate this Entry
by , 11-01-2011 at 05:48 PM (1008 Views)
LinkedList has a benefit over ArrayList/Vector when it comes to insert and delete operation. LinkedLists can add and delete any element in the list very efficiently because only the node pointers are to be updated. While talking about the memory consumption, each element/object in a LinkedList takes a bit more memory as compared to ArrayList/Vector because of the pointers involved.



ArrayList, Vector and LinkedList all have their importance. The important thing is to use them in the right situation.

In the example below, we created a LinkedList and added 2 objects to it. We then added a 3rd object at the first index using addFirst() method. This will add the new node to the first place and only few pointers are readjusted. This operation in LinkedList is very efficient as compared to ArrayList/Vector.

Java Code:
LinkedList ll = new LinkedList();
ll.add("Pluto");
ll.add("Neptune");
ll.addFirst("Mercury");

for(int i=0;i
System.out.println(ll.get(i));

Submit "LinkedList - insertion/deletion" to Facebook Submit "LinkedList - insertion/deletion" to Digg Submit "LinkedList - insertion/deletion" to del.icio.us Submit "LinkedList - insertion/deletion" to StumbleUpon Submit "LinkedList - insertion/deletion" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments