List in Java contains index-based methods. This enables us to maintain the order collection. So this enables us to search, insert, delete, and even update the elements. This enables us to store multiple copies of the same element already existing on our list. Also, in addition, null elements are allowed to be a part of the List.
We access the list through their index numbers called Interface that we won’t be discussing here.
Types of List
ArrayList is used where elements to be inserted are known because there is no flexibility once we have declared the ArrayList but is frequently used as operation over elements are much faster and the good part of Arraylist is we can directly access the element through ArrayList interface.
The syntax for ArrayList :
ArrayList<String> cars = new ArrayList<String>();
LinkedList is preferred over Arraylist if we want a flexible list with no size constraint and operations over elements are quite slower.
LinkedList<E> extends AbstractList<E> implements List<E>, Deque<E> ;
Vector method is similar to Arraylist just Vector has an edge over ArrayList because all elements in vectors are synchronized and are only useful if making multithreaded applications. So, in practice vector class isn‘t used more frequently anymore.
Vector object= new vector(datatype parameter1, datatype parameter2, ...., datatype parameterN)
Sublist is a portion of List
The subList() method of java.util.ArrayList class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all the optional list operations.
public List subList(int fromIndex, int toIndex)
Parameters: This method takes the following argument as a parameter.
- fromIndex: low endpoint (inclusive) of the subList
- toIndex: high endpoint (exclusive) of the subList
Return type: A view of the specified range within this list.
Exception: This method throws the following Exception.
- IndexOutOfBoundsException – if an endpoint index value is out of range (fromIndex size)
- IllegalArgumentException – if the endpoint indices are out of order (fromIndex > toIndex)
Original arrlist: [1, 4, 9, 25, 36] Sublist of arrlist: [9, 25]
Original arrlist: [Example, in, Geeks, for, Geeks] Sublist of arrlist: [Geeks, for, Geeks]
Example 3: For IllegalArgumentException
Original arrlist: [Example, in, Geeks, for, Geeks] Endpoint indices are out of order (fromIndex > toIndex) Exception thrown: java.lang.IllegalArgumentException: fromIndex(9) > toIndex(3)
Example 4: For IndexOutOfBoundsException
Original arrlist: [1, 4, 9, 25, 36] End index value is out of range Exception thrown : java.lang.IndexOutOfBoundsException: toIndex = 7
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.