LinkedList vs Arraylist
If we look from outside we may not find much difference between Arraylist and LinkedList but there definately are major differences.
ArrayList internally uses a dynamic array to store the elements.LinkedList internally uses a doubly linked list to store the elements.
Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. For example If there are 3 items in an Arraylist at index position 0,1,2 and we add new item at index 1 like myarraylist(1,”new item at index 1”) then items at positions 1 and 2 will move to index 2 and 3. So if there are 1000s of items in an ArrayList they will all move one index down and need lots of computations. If use Linked list where each item is just linked to next item and the previousitem .so if we add an item at position 1 by using the above method then all that will happen is item at position 0 will detach from old item at position 1 and attach to new item at position 1 and in same way new item will now attach to item that was earlier at position 1.
Another difference is be can iterate through items of linkedllist using iterator or Listiterator.
Linkedlists are defined like this
LinkedList<String> stringLinkedList = new LinkedList();
we can then add items to the list like this and they will be added to the list at positions 0,1,2,3 etc
stringLinkedList.add("String at position 0");
stringLinkedList.add("String at position 1");
stringLinkedList.add("String at position 2");
we can add item at particular index as follows later on and other items will move one index below the list
stringLinkedList.add(3,"abcbcbcbcbcbcb");
we can remove item at a particular index as well and all items will move one index up.
stringLinkedList.remove(5);
We can iterate through all values of linkedlist using iterator
Iterator it = myLinkedList.iterator()
while(it.hasNext()){
System.out.println("The current string is "+it.next());
}
String List iterator is defined by the following format where stringListIterator is the name we can provide and myLinkedList is name be gave to our String LinkedList
ListIterator<String> stringListIterator = myLinkedList.listIterator();