Differences Between Java List vs Array List : Brief comparision

Differences Between Java List vs Array List

Java is a dynamic language and can be used on any platform. It provides a Java List vs ArrayList. List acts as an interface and Array list is an implementation of the list. List interface consists of methods. These methods are included in the Array list class with a few additions of methods. The main difference between Java List vs ArrayList is that you need to create a reference to the parent interface in the first one and a reference to the class which implements the list. That means Array list class is the second class. Let us have a look at the differences between Java List vs ArrayList.

aquad website useful links

Head to Head Comparison Between Java List vs Array List (Infographics)

Below Is The Top 4 Comparison Between Java List vs Array List

Key Differences between Java List vs Array List

The Differences Between Java List vs Array List are explained in the below-mentioned points:

  • One of the major difference is between Java List vs Array List is that list is an interface and Array list is a standard collection class.
  • Java List interface extends Collection and Array list extends Abstract List class and it can also implement List interface.
  • List interface creates a collection of elements which are stored in sequence and can be accessed by its index number. Array list, on the contrary, creates an array of objects where the array can grow dynamically whenever required and reduce as well.
  • Both Java List vs Array List provides different kinds of methods to access data from the list. These methods enable in getting elements from an array at the specified position and also remove and shrink the size of an array in case of the Array list.

Java List vs Array List Comparisons Table

Following is the comparison table between Java List vs Array List

The basis of Comparison Between Java List vs Array List Java List Array List
Basic difference List is an interface which extends Collection. As it extends Collection it declares its behavior and stores a sequence of elements. A list can have some additional methods along with the ones present in Collection. A list can also contain duplicate elements. Many of the methods in a list can throw Unsupported Operation Exception if the collection cannot be modified. Array List is a class which extends Abstract List and it implements the List interface. Normal arrays that are used in Java are of fixed length. Once an array is created in Java its size cannot be increased or decreased. Array lists are dynamic and can be created with an initial size and later the size can be increased if more data is added and also it can be decreased if any data is removed.
Syntax The syntax to declare a list is as follows:
public interface List<E> extends Collection<E>
The syntax to declare Array list is as follows:
public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable
Working Java List extends Collection framework and uses namespace system.collection.generic. A list is used to get objects that are related to their index numbers. A list object can be created as below:
List a= new ArrayList();
Once an object is created by a user can restrict the type of object which can be stored in the list. An object can be declared as type safe. This can be done as below:
// Obj is a type of object to be stored in List.List list = new List ();
Array list is created by extending AbstractList and implements the list interface. The namespace used by Array list is System. Collections. The array list is considered when user needs to create a dynamic array that contains objects and which can be added and removed at runtime.
ArrayList is a class and hence provides a few constructors. These constructors are as below:
1)ArrayList(): Using this constructor user can create an empty array list.2)ArrayList(Collection c): This constructor is used to create an array list which is initialized with the elements which are sent in collection c.3)ArrayList(int capacity): The main feature of Array list is that it increases automatically when more elements are added to the array list. This constructor helps to create an array list with a specified initial capacity.
Methods A list provides various methods:1) void add(int index, Object obj)
This method helps in inserting objects. This is done by invoking list at the index that is being passed. Any existing elements are shifted and as a result, no data is overwritten.2) Object get(int index)
It returns the object which is stored at that particular index within the invoking collection.3) ListIterator listIterator()
This function returns an iterator to the start of invoking list.

4) Object remove(int index)
It is used in removing elements at the mentioned position index and returns the deleted item. Also, the resulting list is compressed and indexes of subsequent elements are decremented by one.

5) List subList(int start, int end)
It will return a list from start to end where the start is the starting index and end will be the ending index. Elements in a list can also be referenced by the invoking object.

An array list provides below methods:
1) void add(int index, Object obj)
It inserts a specified element at a specified position and will throw an exception IndexOutOfBoundsException when the mentioned index is out of range.2)Boolean contains(Object o)
It searches the list and returns true if the element specified is present. To be more specific if the list contains at least one element such that o==null? e==null : o.equals(e)3)Object get(int index)
It returns the element present at the specified position and will throw an exception IndexOutOfBoundsException when the mentioned index is out of range.4) void ensureCapacity(int minCapacity)
It increases the capacity of Array list instance. It makes sure that it can hold a minimum number of elements which is specified as the capacity.5)Object[] toArray()
It returns an array which consists of all elements in list. It returns NullPointerException if the array is null.

Conclusion –  Java List vs Array List :

As a result, array list is the solution to the problem of a memory of static array. When a user uses an array list, the dynamic array is created which can be increased whenever required. The normal list extends collection class. Also, it is better to increase the capacity of an array in the beginning than reallocating the memory later. Manipulation is slow in array list as shifting needs to be done if any element is removed from a list. In a list you can perform different operations of positional access, searching and range view as well.

Leave a Reply

Your email address will not be published. Required fields are marked *