Is ArrayList a class in Java?
Java ArrayList is perhaps the simplest and one of the most used data structure implementation classes of the Java API Library. It is a part of the Java Collection Framework under the java.util package. On one hand, it behaves like a normal array, providing all the benefits of it and, on the other, it is a generic re-sizable collection implementation of the List interface. Java ArrayList is especially used for managing a large number of objects. This article attempts to provide some information of this utility class with a brief overview of its structure. Show Java ArrayList Class HierarchyIn short, Java ArrayList is a subclass of AbstractList and implements the List interface, and List is an extension of the Collection interface. As a result, we can declare an ArrayList object as any of the ways that follow:
Figure 1: An ArrayList chart Refer to the Java API Documentation for more details on the classification. Java ArrayList OverviewWhile dealing with data structures in any programming language, we resort to low-level manipulation of data. Creating each element of the data structure dynamically and modifying them by directly manipulating references to it can be erroneous and also daunting at times. Direct manipulation may be necessary on occasion, yet when productivity is concerned, support from the API library comes in quite handy. Java Collection Framework contains many such prepackaged data structures, interfaces, and algorithms. Java ArrayList is one of them and can be used like the low-level arrays with some special connotation. Java ArrayList uses an array as the internal programming construct to store elements. An array is nothing but a sequential collection same type of elements, accessed by their index values. Naturally, Java ArrayList also behaves in a similar manner. Here, the behavior is defined by the ready-made methods of the ArrayList class. Common operations are add/remove elements in various ways, determine if the list is empty, obtain the size of the list indicated by number of elements present currently, and so forth. Java ArrayList ConstructorsJava ArrayList class contains three constructors, such as:
Java ArrayList MethodsThere are various methods. A few of the commonly used are as follows:
A Quick Examplepackage org.mano.example; import java.util.ArrayList; import java.util.Random; public class ArrayListDemo1 { public static void main(String[] args) { ArrayListHow to Use the Java ArrayList add() MethodHere is another example showing how to specifically use the Java ArrayList add() method to add elements to an ArrayList object: import java.util.ArrayList; public class ArrayListDemo2 { public static void main(String[] args) { ArrayListWhen you run the above code, you will see two sets of output. The first shows the elements of the first iteration of planets. Then, we add Uranus as an array element and print out planets again to show that it was, indeed, added. Generic ArrayList in JavaJava ArrayList is built to support generic types. That is the reason that the class refers to having elements of type E, such as ArrayList Similarly, for Employee object, we may write: ArrayListWe also may create an ArrayList without specifying a type, such as: ArrayList unknownList=new ArrayList<>();This, however, is not always a good practice because we should let the compiler know the type of data element prior to adding it to the list. The ? denotes a wild card. Let us illustrate a case to get a clear idea about the uses of the generic ArrayList, including wildcards. Suppose we want to implement a list of numbers and a generic sumTotal() method to get a total sum of the number of elements in the collection. By definition, generic classes can be used only with class or interface types. As a result, the numbers should be auto-boxed as objects of a wrapper class, such as an int value would be auto-boxed as Integer or a double value would be auto-boxed as a Double wrapper class. Our sumTotal() function should be able to calculate the total of the numbers in the ArrayList regardless of type variation. Therefore, well declare the ArrayList as ArrayList So far, so good. The sumTotal() method takes a parameter as ArrayList The compiler does not consider the parameterized type ArrayList Under these circumstances, can we create a sumTotal() method that is flexible enough to work with any subclass of Number? The answer is, we can, but by using wild card type arguments. Lets modify the preceding example to suit our needs. package org.mano.example; import java.util.ArrayList; public class Main { public static void main(String[] args) { Integer[] nos = { 10, 20, 89, 22 }; ArrayListConclusionThis article tried to put up some specific points in using the Java ArrayList class. In simple terms, ArrayList is an implementation of the concept of dynamic arrays. Normal arrays are of a static length; they cannot grow or shrink. That means we must specify their capacity prior to using them. Java ArrayList, in essence, is a variable-length array of object references that can dynamically increase or decrease its capacity. It is created with an initial size but, if this size is exceeds, the collection is automatically enlarged. Conversely, when objects are removed, the array is also shrunk. |