Java – ArrayList and LinkedList

ArrayList and LinkedList in Java

I have decided to treat ArrayList and LinkedList together. This is because they are basically the same from the programmer’s view. So you use almost the same syntax for both. We would see this in a minute.

Also note that ArrayList and LinkedList both inherites from the List class. So I would refer to both of them as just List.

A list stores a sequence of elements which could be of the same or of different data types.

 

You probably would want to know ‘difference between ArrayList and Array‘. The main difference is that ArrayList support different data types while Array support just one data type.

Read about Arrays in Java Here

 

Properties of Lists

Take note of the following properties of a List. It applies both to ArrayList and LinkedList.

  • You can reference element in a list using the index position of the element in the list. Indexes start from 0.
  • Duplicate elements are allowed in a list.
  • List provide several methods used to manipulate the elements

 

ArrayList and LinkedList Methods

So we have methods that apply to both ArrayList and LinkedList. This are given below.

SN Method brief description
1 add(index, obj)

Inserts an item into a list. The element is inserted at the index specified. Elements at the insertion point are shifted up. Therefore, no elements are overwritten.

2 addAll(index, Collection c)

Inserts all the elements of the collection c into the a list at the index passed in the index. Existing elements at or beyond the point of insertion are shifted. Therefore, no elements are overwritten. It returns true if the invoking list changes. It returns false if otherwise.

3 get(int index)

Returns the element stored at the given index.

4 int indexOf(Object obj)

It returns the index of the first instance of obj in a list. If the element, obj,is not an element of the list, then it returns 1

5 int lastIndexOf(Object obj)

Returns the index of the last instance  of an element in a list. If the element, obj is not an element of the list, then it returns 1.

6 ListIterator listIterator( )

It returns an iterator to the start of the list.

7 ListIterator listIterator(int index)

Returns an iterator to the invoking list that begins at the specified index.

8 remove(int index)

It removes the element at the specified index from the list. It then returns the deleted element. The resulting list is then adjusted. Meaning that the indexes of subsequent elements are reduced by one.

9 Object set(int index, Object obj)

Assigns an element,  obj to the location specified by index within the  list.

10 List subList(int start, int end)

Returns a list that includes elements from specified start to end.1 in the a list. Elements in the returned sublist are also referenced by the calling object.

 

Lets’ take and example

I recommend you try this yourself. The codes creates both an ArrayList and a LinkedList.

 

import java.util.ArrayList;
import java.util.LinkedList;

// ** Arrays, ArrayList and Linked List ***************
public class ListDemo {

	public static void main(String[] args) {
		
		ArrayList alist = new ArrayList();		
		alist.add("Osondu");
		alist.add("Saffron");
		alist.add("Oleander");
		alist.add(23.87);
		alist.add('A');
		System.out.println("\nElement of ArrayList: ");
		System.out.println("\t" + alist);
		
		LinkedList llist = new LinkedList();		
		llist.add("Osondu");
		llist.add("Saffron");
		llist.add("Oleander");
		llist.add(23.87);
		llist.add('A');
		System.out.println("\nElement of LinkedList: ");
		System.out.println("\t" + llist);	
	}
}

Listing 1.0: Array List and LinkedList

 

If you execute the code in Listing 1.0, then you will have the output below:

 

Element of ArrayList: 
	[Osondu, Saffron, Oleander, 23.87, A]

Element of LinkedList: 
	[Osondu, Saffron, Oleander, 23.87, A]

 

 

You can therefore see that ArrayLists and LinkedList are very similar.