Difference between List and ArrayList Java

Differences Between Java List and Array List

Java is a dynamic language and can be used on any platform. It provides a Java List vs ArrayList. The list acts as an interface, and an Array list is an implementation of the list. The 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 an Array list class is the second class. Let us have a look at the differences between Java List vs ArrayList.

Head to Head Comparison Between Java List and Array List [Infographics]

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

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Key Differences between Java List and Array List

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

  • One of the major differences is between Java List vs Array List is that list is an interface, and the Array list is a standard collection class.
  • The Java List interface extends the Collection, and the Array list extends the abstract listening class, and it can also implement the List interface.
  • List interface creates a collection of elements that 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 getting elements from an array at the specified position and 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 ComparisonJava ListArray List
Basic differenceThe list is an interface that 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 the 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 that extends the 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 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.
SyntaxThe syntax to declare a list is as follows:

public interface List extends Collection

The syntax to declare an Array list is as follows:

public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable

WorkingJava List extends the Collection framework and uses the 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 [];
An array list is created by extending AbstractList and implements the list interface. The namespace used by the Array list is System. Collections. The array list is considered when the 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.
MethodsA list provides various methods:
1] void add[int index, Object obj]
This method helps in inserting objects. This is done by invoking a 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 the 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 the end will be the ending index. Elements in a list can also be referenced by the invoking object.
An array list provides the 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 the 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 that consists of all elements in the list. It returns NullPointerException if the array is null.

Conclusion

As a result, the array list is the solution to the problem of a memory of a static array. When a user uses an array list, the dynamic array is created, which can be increased whenever required. The normal list extends the 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 the 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.

Recommended Article

This has been a guide to the top differences between Java List vs Array List. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles to Learn more

  1. Java Vector vs ArrayList
  2. Java vs JavaScript
  3. Java Heap vs Stack
  4. C# Array vs List

Java Training [40 Courses, 29 Projects, 4 Quizzes]

40 Online Courses

29 Hands-on Projects

285+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

3 Shares
Share
Tweet
Share

Video liên quan

Chủ Đề