Which field or method of the ArrayList class will return the number of elements stored in the ArrayList?
Why ArrayList is better than Array?The limitation with array is that it has a fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink. Show
On the other ArrayList can dynamically grow and shrink after addition and removal of elements (See the images below). Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. Let’s see the diagrams to understand the addition and removal of elements from ArrayList and then we will see the programs. Adding Element in ArrayList at specified position: Removing Element from ArrayList:
Java ArrayListlast modified November 12, 2021 Java ArrayList tutorial shows how to work with ArrayList collection in Java. Located in the java.util package, ArrayList is an important collection of the Java collections framework. Java collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details. A collection is an object that represents a group of objects. ArrayList Methods In JavaThe following table lists all the methods that are provided by the ArrayList class.
Next, we will discuss each of these methods from the ArrayList function API in detail and present programming examples. After discussing all the methods listed above, we will also take up some specific operations that are carried out using ArrayLists which are not a part of the ArrayList function API. ArrayList addI Prototype: boolean add (E e) II. Prototype: void add (int index, E element) Parameters: index=> Position at which the element is to be added. Return Value: void Description: Adds given element ‘element’ at the specified position ‘index’ by shifting the element at that position and subsequent elements to the right. Exceptions: IndexOutOfBoundsException => If the specified index is out of the range. ArrayList addAllI Prototype: boolean addAll (Collection c) Description: Adds all the elements in the given collection c to the end of the list. The result of the operation is undefined if the collection is altered when the operation is in progress. Exceptions: NullPointerException => If given collection c is null. II Prototype: boolean addAll (int index, Collection c) Description: Adds all the elements in the given collection c at the position specified by the ‘index’ in the list. The element at the specified index and subsequent elements are shifted to the right. The result of the operation is undefined if the collection being added is altered when the operation is in progress. Exceptions: IndexOutOfBoundsException: if the index where the collection is to be added is out of bounds The following Java program demonstrates the usage of add and addAll methods. import java.util.*; class Main{ public static void main(String args[]){ //create an ArrayList ArrayListOutput: Initial ArrayList:[Delhi, Mumbai, Chennai, Kolkata] ArrayList in JavaArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. Illustration: Example: The following implementation demonstrates how to create and use an ArrayList. Java
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
Since ArrayList is a dynamic array and we do not have to specify the size while creating it, the size of the array automatically increases when we dynamically add and remove items. Though the actual library implementation may be more complex, the following is a very basic idea explaining the working of the array when the array becomes full and if we try to add an item:
Important Features:
Let’s understand the Java ArrayList in depth. Look at the below image: In the above illustration, AbstractList, CopyOnWriteArrayList, and the AbstractSequentialList are the classes that implement the list interface. A separate functionality is implemented in each of the mentioned classes. They are:
Constructors in the ArrayListIn order to create an ArrayList, we need to create an object of the ArrayList class. The ArrayList class consists of various constructors which allow the possible creation of the array list. The following are the constructors available in this class: 1. ArrayList(): This constructor is used to build an empty array list. If we wish to create an empty ArrayList with the name arr, then, it can be created as:
2. ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from the collection c. Suppose, we wish to create an ArrayList arr which contains the elements present in the collection c, then, it can be created as:
3. ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified. Suppose we wish to create an ArrayList with the initial size being N, then, it can be created as:
Methods in Java ArrayList
Let’s see how to perform some basics operations on the ArrayList as listed which we are going to discuss further alongside implementing every operation.
Operation 1: Adding Elements In order to add an element to an ArrayList, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters. They are as follows:
Example: Java
Output:
[Geeks, For, Geeks]
Operation 2: Changing Elements After adding the elements, if we wish to change the element, it can be done using the set() method. Since an ArrayList is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index. Example Java
Output:
Initial ArrayList [Geeks, Geeks, Geeks]
Updated ArrayList [Geeks, For, Geeks]
Operation 3: Removing Elements In order to remove an element from an ArrayList, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters. They are as follows:
Example Java
Output:
Initial ArrayList [Geeks, For, Geeks]
After the Index Removal [Geeks, Geeks]
After the Object Removal [Geeks]
Operation 4: Iterating the ArrayList There are multiple ways to iterate through the ArrayList. The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for loop. Example Java
Output:
Geeks For Geeks
Geeks For Geeks
Must Read: Array vs ArrayList in Java
Article Tags :
Java Technical Scripter
Java - util package Java-ArrayList Java-Collections java-list Practice Tags :
Java Java-Collections
Read Full Article
ArrayList size() method in Java with ExamplesThe size() method of java.util.ArrayList class is used to get the number of elements in this list. Syntax: public int size()Returns Value: This method returns the number of elements in this list. Below are the examples to illustrate the size() method. Example 1:
Output:
Before operation: [1, 2, 3, 4, 5]
Size of list = 5
Example 2:
Output:
Before operation: [A, B, C]
Size of list = 3
Article Tags :
Java
Java - util package Java-ArrayList Java-Collections Java-Functions Practice Tags :
Java Java-Collections
Read Full Article
|