Java ArrayListJava ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in the java.util package. It is like the Vector in C++. The ArrayList in Java can have the duplicate elements also. It implements the List interface so we can use all the methods of the List interface here. The ArrayList maintains the insertion order internally. It inherits the AbstractList class and implements List interface. The important points about the Java ArrayList class are:
Hierarchy of ArrayList classAs shown in the above diagram, the Java ArrayList class extends AbstractList class which implements the List interface. The List interface extends the Collection and Iterable interfaces in hierarchical order. ArrayList class declarationLet's see the declaration for java.util.ArrayList class. Constructors of ArrayList
Methods of ArrayList
Java Non-generic Vs. Generic CollectionJava collection framework was non-generic before JDK 1.5. Since 1.5, it is generic. Java new generic collection allows you to have only one type of object in a collection. Now it is type-safe, so typecasting is not required at runtime. Let's see the old non-generic example of creating a Java collection. Let's see the new generic example of creating java collection. In a generic collection, we specify the type in angular braces. Now ArrayList is forced to have the only specified type of object in it. If you try to add another type of object, it gives a compile-time error. For more information on Java generics, click here Java Generics Tutorial. Java ArrayList ExampleFileName: ArrayListExample1.java Test it NowOutput: [Mango, Apple, Banana, Grapes] Iterating ArrayList using IteratorLet's see an example to traverse ArrayList elements using the Iterator interface. FileName: ArrayListExample2.java Test it NowOutput: Mango Apple Banana Grapes Iterating ArrayList using For-each loopLet's see an example to traverse the ArrayList elements using the for-each loop FileName: ArrayListExample3.java Output: Test it NowMango Apple Banana Grapes Get and Set ArrayListThe get() method returns the element at the specified index, whereas the set() method changes the element. FileName: ArrayListExample4.java Test it NowOutput: Returning element: Apple Mango Dates Banana Grapes How to Sort ArrayListThe java.util package provides a utility class Collections, which has the static method sort(). Using the Collections.sort() method, we can easily sort the ArrayList. FileName: SortArrayList.java Output: Apple Banana Grapes Mango Sorting numbers... 1 11 21 51 Ways to iterate the elements of the collection in JavaThere are various ways to traverse the collection elements:
Iterating Collection through remaining waysLet's see an example to traverse the ArrayList elements through other ways FileName: ArrayList4.java Output: Traversing list through List Iterator: Ajay Ravi Vijay Ravi Traversing list through for loop: Ravi Vijay Ravi Ajay Traversing list through forEach() method: Ravi Vijay Ravi Ajay Traversing list through forEachRemaining() method: Ravi Vijay Ravi Ajay User-defined class objects in Java ArrayListLet's see an example where we are storing Student class object in an array list. FileName: ArrayList5.java Output: 101 Sonoo 23 102 Ravi 21 103 Hanumat 25 Java ArrayList Serialization and Deserialization ExampleLet's see an example to serialize an ArrayList object and then deserialize it. FileName: ArrayList6.java Output: [Ravi, Vijay, Ajay] Java ArrayList example to add elementsHere, we see different ways to add an element. FileName: ArrayList7.java Output: Initial list of elements: [] After invoking add(E e) method: [Ravi, Vijay, Ajay] After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay] After invoking addAll(Collection<? extends E> c) method: [Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addAll(int index, Collection<? extends E> c) method: [Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] Java ArrayList example to remove elementsHere, we see different ways to remove an element. FileName: ArrayList8.java Output: An initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav] After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav] After invoking remove(index) method: [Ajay, Anuj, Gaurav] Updated list : [Ajay, Anuj, Gaurav, Ravi, Hanumat] After invoking removeAll() method: [Ajay, Anuj, Gaurav] After invoking removeIf() method: [Anuj, Gaurav] After invoking clear() method: [] Java ArrayList example of retainAll() methodFileName: ArrayList9.java Output: iterating the elements after retaining the elements of al2 Ravi Java ArrayList example of isEmpty() methodFileName: ArrayList4.java Output: Is ArrayList Empty: true After Insertion Is ArrayList Empty: false Java ArrayList Example: BookLet's see an ArrayList example where we are adding books to the list and printing all the books. FileName: ArrayListExample20.java Test it NowOutput: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications and Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Size and Capacity of an ArrayListSize and capacity of an array list are the two terms that beginners find confusing. Let's understand it in this section with the help of some examples. Consider the following code snippet. FileName: SizeCapacity.java Output: The size of the array is: 0 Explanation: The output makes sense as we have not done anything with the array list. Now observe the following program. FileName: SizeCapacity1.java Output: The size of the array is: 0 Explanation: We see that the size is still 0, and the reason behind this is the number 10 represents the capacity no the size. In fact, the size represents the total number of elements present in the array. As we have not added any element, therefore, the size of the array list is zero in both programs. Capacity represents the total number of elements the array list can contain. Therefore, the capacity of an array list is always greater than or equal to the size of the array list. When we add an element to the array list, it checks whether the size of the array list has become equal to the capacity or not. If yes, then the capacity of the array list increases. So, in the above example, the capacity will be 10 till 10 elements are added to the list. When we add the 11th element, the capacity increases. Note that in both examples, the capacity of the array list is 10. In the first case, the capacity is 10 because the default capacity of the array list is 10. In the second case, we have explicitly mentioned that the capacity of the array list is 10. Note: There is no any standard method to tell how the capacity increases in the array list. In fact, the way the capacity increases vary from one GDK version to the other version. Therefore, it is required to check the way capacity increases code is implemented in the GDK. There is no any pre-defined method in the ArrayList class that returns the capacity of the array list. Therefore, for better understanding, use the capacity() method of the Vector class. The logic of the size and the capacity is the same in the ArrayList class and the Vector class.Related TopicsDifference between Array and ArrayList When to use ArrayList and LinkedList in Java Difference between ArrayList and LinkedList Difference between ArrayList and Vector How to Compare Two ArrayList in Java How to reverse ArrayList in Java When to use ArrayList and LinkedList in Java How to make ArrayList Read Only Difference between length of array and size() of ArrayList in Java How to Synchronize ArrayList in Java How to convert ArrayList to Array and Array to ArrayList in java How to Sort Java ArrayList in Descending Order How to remove duplicates from ArrayList in Java
Next TopicJava LinkedList
|