Insert and Delete in ArrayList

In this tutorial we will see, How can you insert and delete in ArrayList in Java.

Unlike Array In Java, ArrayList is a dynamic data structure. ArrayList can increase and decrease its size at runtime, We don’t need to declare the ArrayList’s size at the time of ArrayList implementation. It’s also shrinking in memory if you remove the elements from ArrayList. This is the biggest benefit of ArrayList over Arrays.

It’s very easy to add the elements in the ArrayList and remove the elements from ArrayList.

 

Add Elements in ArrayList —

It’s very simple to add the elements in the ArrayList. To add the elements in the ArrayList we use inbuilt method add(element value) of ArrayList class.

You just need to create the object of the ArrayList class and with the reference of an object you can use the add() method to add the elements in the ArrayList.

 

Remove Elements in ArrayList —

It’s very simple to remove the elements in the ArrayList. To remove the elements in the ArrayList we use inbuilt method remove(element index) of ArrayList class.

You just need to create the object of the ArrayList class and with the reference of the object, you can use the remove() method to remove the elements in the ArrayList.

Let’s understand this with an easy example —

Output:

Elements of the ArrayList are — [10, 15, 20]
Elements of Array after removal index 2 element –[10, 15]

 

In the above example, First we create the ArrayList class object then using add() method we performed the elements insertion and print the arrayList.

After the using remove() method , we deleted the elements from the ArrayList and again print the ArrayList.

Using this way we can perform the insertion and deletion from the ArrayList collection class.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply