ArrayList in Java
In the previous blog we learned about the Object class in Java. If you want to know more about the Object class then click on Object Class in Java. In this blog, we will learn about the ArrayList in Java. In C and C++ the size of arrays is fixed at compile time only, we cannot change it. Suppose there is a department in each department there will be not more than 100 students. But what if one department has 150 students, then defining an array of 150 for every department is not a good way, because just for 1 department we will have to define an array of 150 which is not an appropriate solution. To overcome this, Java allows to set the size of an array at runtime.
e.g.
But the above statement doesn’t completely solve the problem of dynamically modifying the array at runtime. Once the size of an array is set we cannot change size of an array easily. But we can deal with this with another approach in Java which is called ArrayList. ArrayList is similar to an array, but it has the ability to adjust its capacity when the elements are added or removed, no extra code is required. We will go through ArrayList in detail:
ArrayList Declaration
If we want declare an ArrayList of Student object, then we can do it by the following way:
If we don’t want to use the var keyword, then we can eliminate the type parameter on the right -hand side:
The above syntax is called diamond syntax because the empty brackets(<>) depict a diamond. The compiler checks what happens to the new value. If the new value is assigned to a variable, passed into a method, or returned from a method, then the compiler checks the generic type of the variable, parameter, or method. It places that particular type into the diamond(<>). In the above example, the new ArrayList<>() is assigned to a variable of type ArrayList <AtrowelStudent>. Therefore, the generic type is AtrowelStudent.
Methods
- add() - We use the add() method to add the elements into an array list. Following is the example for populating the array list:
- ensureCapacity() - If we know the count of elements to be stored i.e. if we know how many elements will be stored, then we can use the ensureCapacity() method. For example
- size() - The size method returns the actual number of elements in an array list.
It(array list) manages an internal array of object references. It is obvious that after some time the array will not have space i.e. it will run out of space. This is when the ArrayList plays an important role. If we call add() method and suppose if the internal array is full, then the array list automatically creates a bigger array and copies all the objects from the smaller to the bigger array.
We can also initial capacity to the ArrayList . For example:
e.g.
It returns the number of elements in the atrowelStaff array list. It is the same as using the length() method.
e.g.
Summary of Methods used in ArrayList
Methods | Description |
---|---|
boolean add(AtrowelEmployee obj) | It appends obj at the end of the array list and always returns true. |
int size() | It returns the number of elements currently stored in the array list. |
void ensureCapacity(int capacity) | It ensures that the array list has the capacity to store the given number of elements without reallocating its internal storage array. |
void trimToSize() | It reduces the storage capacity of the array list to its current size. |
Accessing ArrayList Elements in Java
Accessing the array elements from an ArrayList is not an easy task. It follows a complex syntax for accessing the elements. The reason behind it is that the ArrayList class is not a part of the Java programming language; it is a utility class programmed by someone and supplied in the standard library. In order to access the elements from the ArrayList get() method and set() method is used. For example, to set the particular element i.e. ith element we use:
The above statement is equivalent to:
Sometimes we get the growth flexible growth and convenient element access by just using the following trick: First, we have to make an array list and add all the elements:
Once the above step is done, use the toArray method to copy the elements into an Array:
Sometimes the situation occurs where we have to add elements in the middle of an array list, then we can use the add method with an index parameter:
The elements at locations atrowelNo and above are shifted up, in order to make some space for new entry. Suppose if the new size of an array list after insertion exceeds the capacity, then the array list can reallocate its storage array. We can also remove an element from the middle of an array list by using the following method:
For traversing the content of an array list we can use for each loop. For example:
The above statement behaves same as the for loop i.e.:
e.g.
Summary of Methods used in ArrayList for accessing elements from the array list
Methods | Description |
---|---|
AtrowelEmployee set(int index, AtrowelEmployee atrowelObj) | It puts the value atrowelObj in the array list at the specified index, returning the previous contents. |
AtrowelEmployee get(int index) | It gets the value stored at a specified index. |
void add(int index, AtrowelEmployee atrowelObj) | It shifts up elements to insert atrowelObj at the specified index. |
AtrowelEmployee remove(int index) | It removes the element at the given index and shifts down all elements above it and returns the removed element. |