Example of using ArrayList
In the previous post, we have see how to initialize the ArrayList. In this post, we will example of using ArrayList.
ArrayList is a class which implements List Interface. It is an good alternative of Java Arrays. There are few benefits of using ArrayList over Java Arrays.
- Java Arrays are of fixed length and if they are full we can not add more elements to it. But ArrayList can dynamically grow. You can add as many as elements you want.
- If some elements are removed from java array, even after removal memory consumption would be the same as it does not shrink. But ArrayList can be shrink easily as per the need.
- Apart from this ArrayList provides predefined methods to work on the elements which makes life easy, but this is not the case with array.
Let’s understand the ArrayList and its methods with examples.
1)add method:
This method is used to add objects to the ArrayList.
Example: We are adding cities to the arrayList.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); System.out.println("All city Names: "+Banklist); } }
Output:
2) add(int index, ObjType element): It allows you to add any element of specific type at the desired index. Index starts from zero (0).
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); Banklist.add(0, "Mumbai"); System.out.println("All city Names: "+Banklist); } }
Output:
3) remove(Object o): It allows you to remove the specified object.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); Banklist.remove("Pune"); System.out.println("All city Names: "+Banklist); } }
Output:
4) remove(int index): It allows to remove the element specified at a particular index at run time.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); Banklist.remove(1); System.out.println("All city Names: "+Banklist); } }
Output:
5) contains(Object o): It verifies that a ArrayList contain the specified object or not and return boolean value true/false.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); if(Banklist.contains("Pune")){ System.out.println("Object exist : Pune"); } } }
Output:
6) set(int index, Object o): This method allows you to update the object at a particular index.In the below example, we are updating 2nd index value from Bangalore to Mumbai.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); Banklist.set(1, "Mumbai"); System.out.println("All Cities Name: "+Banklist); } }
Output:
7) Object get(int index): It returns the object specified at a particular index.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); System.out.println("City at index 1 : "+Banklist.get(1)); } }
Output:
8) 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.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); System.out.println("Pune is present at index : "+Banklist.indexOf("Pune")); } }
Output:
9) int size(): It returns the size of the list i.e. number of elements present in the list.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); System.out.println("Total Elements : "+Banklist.size()); } }
Output:
10) clear(): This method will remove all the objects in the list.
import java.util.ArrayList; public class ArrayLIstEx { public static void main(String args[]){ ArrayList<String> Banklist = new ArrayList<String>(); Banklist.add("Delhi"); //adding item Banklist.add("Bangalore"); Banklist.add("Pune"); Banklist.clear(); System.out.println("Total Elements : "+Banklist.size()); } }
Output: