ArrayList is an inbuilt java class available in java.util package. It implements the List interface.
ArrayList acts like an array, but it has extra features over array which make it very useful.
Features of ArrayList are :
1. ArrayList is growable. The size of ArrayList is not fixed as it was in array we can store as many elements as we want.when new element require to store in ArrayList and there is no place in ArrayList , it java automatically increase the size of ArrayList.
2. ArrayList can store any kind of object. ArrayList is collection of heterogeneous objects. We can store integer, double, string data in a single array list.
Example:
1. Declare an ArrayList
We can declare an ArrayList by just using new key word.
2. Add elements into ArrayList
To add elements into ArrayList, we can use method add(). This is also an inbuilt method of ArrayList Class.
You can see I added three different values into ArrayList object dummyList.
3. Access elements from ArrayList
All elements are added successfully into ArrayList. Now you can access these elements by get() method. We can access elements by using the index values of objects which start from 0 same as in array.
dummyList.get(0) will return the first object stored at index 0.
size() method returns the number of elements situated in ArrayList.
This loop will print all the objects stored in list.
4.OutPut
You should try it by your self. I will post other uses of ArrayList in my next post.
I hope it will help you to get an overview of ArrayList.
Your valuable feedback will surely help me to improve.