Arraylist is a class which implements List interface. It is widely used because of the functionality and flexibility it offers. Most of the developers choose Arraylist over Array as it’s a very good alternative of traditional java arrays.. Let’s see the ArrayList example first then we will discuss it’s methods and their usage.
ArrayList Example in Java
import java.util.*; public class ArrayListExample { public static void main(String args[]) { /*Creation of ArrayList: I'm going to add String *elements so I made it of string type */ ArrayList<String> obj = new ArrayList<String>(); /*This is how elements should be added to the array list*/ obj.add("Ajeet"); obj.add("Harry"); obj.add("Chaitanya"); obj.add("Steve"); obj.add("Anuj"); /* Displaying array list elements */ System.out.println("Currently the array list has following elements:"+obj); /*Add element at the given index*/ obj.add(0, "Rahul"); obj.add(1, "Justin"); /*Remove elements from array list like this*/ obj.remove("Chaitanya"); obj.remove("Harry"); System.out.println("Current array list is:"+obj); /*Remove element from the given index*/ obj.remove(1); System.out.println("Current array list is:"+obj); } }
Output:
Currently the array list has following elements:[Ajeet, Harry, Chaitanya, Steve, Anuj] Current array list is:[Rahul, Justin, Ajeet, Steve, Anuj] Current array list is:[Rahul, Ajeet, Steve, Anuj]
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.
1) add( Object o): This method adds an object o to the arraylist.
obj.add("hello");
This statement would add a string hello in the arraylist at last position.
2) add(int index, Object o): It adds the object o to the array list at the given index.
obj.add(2, "bye");
It will add the string bye to the 2nd index (3rd position as the array list starts with index 0) of array list.
3) remove(Object o): Removes the object o from the ArrayList.
obj.remove("Chaitanya");
This statement will remove the string “Chaitanya” from the ArrayList.
4) remove(int index): Removes element from a given index.
obj.remove(3);
It would remove the element of index 3 (4th element of the list – List starts with o).
5) set(int index, Object o): Used for updating an element. It replaces the element present at the specified index with the object o.
obj.set(2, "Tom");
It would replace the 3rd element (index =2 is 3rd element) with the value Tom.
6) int indexOf(Object o): Gives the index of the object o. If the element is not found in the list then this method returns the value -1.
int pos = obj.indexOf("Tom");
This would give the index (position) of the string Tom in the list.
7) Object get(int index): It returns the object of list which is present at the specified index.
String str= obj.get(2);
Function get would return the string stored at 3rd position (index 2) and would be assigned to the string “str”. We have stored the returned value in string variable because in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer variable.
8) int size(): It gives the size of the ArrayList – Number of elements of the list.
int numberofitems = obj.size();
9) boolean contains(Object o): It checks whether the given object o is present in the array list if its there then it returns true else it returns false.
obj.contains("Steve");
It would return true if the string “Steve” is present in the list else we would get false.
10) clear(): It is used for removing all the elements of the array list in one go. The below code will remove all the elements of ArrayList whose object is obj.
obj.clear();
Thank you very much for your Beginner’s Book. It is better than any other source I have found for a Java beginner. The examples are especially helpful. I wish you success.
Thanks for the appreciation. Glad you liked it.
Please how do I get the pdf of your material. Thank a lot
Hi ,
The examples and concepts are explained very nicely and well organized.
Thank you!!
Hi sir! ur way of explanation awesome.this site is best for the java beginners .keep posting…
Thank you so much for posting these contents. The examples given here are best to understand any concept. Keep adding more examples. Thanks again :)
Friend,I am not able to Command Array List showing me error: The type ArrayList is not generic; it cannot be parameterized with arguments
I suspect you imported some different ArrayList class in your classpath.Try import java.util.ArrayList, and I’m sure it would certainly work. Check your Java version as well, ArrayList is not a legacy class so it might support from JDK 1.5
Priya.
Example are clear and easy to understand quickly.
The way of presentation is really very nice.
Thanks 😃
i just stumbled on your website on google, never knew this kind of well explanatory java website exist. Your topics are truly helpful for a java starter(Beginners). i will directed my beginners here.Thanks to you.
Hi, things here are very clear to understand. It is very good for the beginners like me.
why can’t we add elements outside of main() method?
say there are duplicate elements in the list. If I call the method remove(), then will it remove all the duplicate elements?
I tried to do this and for example if you have it like this:
If you have the ArrayList created let`s say like this:
ArrayList list = new ArrayList();
and you set it like this:
position 0 – “John”
1 – “Michael”
2 – “Mitch”
3 – “Gus”
4 – “John”
5 – “Johnny”
if you say: list.remove(“John”); then it will remove its first occurance.
So, the new elements will be like this:
0 – “Michael”
1 – “Mitch”
2 – “Gus”
3 – “John”
4 – “Johnny”
I hope I was clear enough. :)
it’s really very helpful to understand within less Time..too Good :)
So glad i came here, please do you have learning Java as a whole in PDF format?