ArrayList in java

Arraylist is a class which implements List interface. It is widely used because of the functionality and flexibility it offers.

1)The issue with arrays is that they are of fixed length so if it is full we 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.

2) But ArrayList can dynamically grow and shrink as per the need. Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. Let’s see the ArrayList example first then we will discuss it’s methods and their usage.

ArrayList Example

Methods of ArrayList class

In the above example we have used methods such as add and remove. However there are number of methods available which can be used directly using object of ArrayList class. Let’s discuss few of the important methods.

How to loop ArrayList

There are four ways to loop ArrayList:

1. For Loop

2. Advanced for loop

3. While Loop

Last updated