ArrayList in java with example programs
Java Collections Tutorial List :
1:Introduction to Java Collections Framework 2:ArrayList in java with example programs 3:ArrayList Important methods 4:How to sort ArrayList in Java 5:Comparator and Comparable Interface in Java
ArrayList in Java :
ArrayList is subclass of List interface.ArrayList in Java is most frequently used collection class because of the functionality and flexibility it offers. Most of the developers choose ArrayList over Array to store elements.
The main issue with Array is that length of Array is fixed, so if it is full we cannot add more elements to it, also if you are removing elements from Array memory consumption will be same as it will not shrink. At the same time ArrayList can dynamically grow and shrink as per the requirement.
Since we can not modify size of an array after creating it, we prefer to use ArrayList in Java which re-size itself automatically once it gets full.
Characteristic of ArrayList
- ArrayList can contain duplicate elements.
- ArrayList will maintain insertion order.
- Array list objects are not synchronized by default.
- Random access of elements because ArrayList works at the index basis.
Example of Java ArrayList class :
package com.example; import java.util.ArrayList; public class ArrayListExample { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); /* Add some elements to the array list */ list.add("Vicky"); list.add("Raj"); list.add("Anshu"); list.add("Sanu"); list.add("Bala"); /* Displaying array list elements */ System.out.println("Elements of ArrayList :" + list); /* Add element at the given index */ list.add(0, "Mukesh"); list.add(1, "Bablu"); /* Remove elements from array list like this */ list.remove("Bala"); list.remove("Raj"); System.out.println("Now Current element is:" + list); /* Remove element from the given index */ list.remove(2); System.out.println("Now Current element is:" + list); } }
Output
Elements of ArrayList :[Vicky, Raj, Anshu, Sanu, Bala] Now Current element is:[Mukesh, Bablu, Vicky, Anshu, Sanu] Now Current element is:[Mukesh, Bablu, Anshu, Sanu]
Methods of ArrayList class :
In the above example we have used methods such as add and remove. There are number of methods available which can be used directly using object of ArrayList class. Some of the important method of ArrayList is given below.
1) add( Object obj): This method adds an object obj to the ArrayList.
obj.add("hello");
This statement would add a string hello in the arraylist at last position.
2) add(int index, Object obj): This method adds an object obj to the given index of ArrayList.
obj.add(2, "Java");
code mentioned above will add the string java to the 2nd index (3rd position as the array list starts with index 0) of array list.
3) remove(Object o): This method Removes the object obj from the ArrayList.
obj.remove("Mukesh");
code mentioned above will remove the string "Mukesh" from the ArrayList.
4) remove(int index): This method removes element from a given index.
obj.remove(3);
Code mentioned above will remove the element of index 3 (4th element of the list).
5) set(int index, Object obj): This method will be used for updating the element in ArrayList.It replaces the element present at the specified index with the object obj.
obj.set(2, "Ravi");
Code mentioned above will replace the 3rd element (index =2 is 3rd element) with the value Ravi.
6) int indexOf(Object obj): This method will return the index of the object obj. If the element is not found in the list then this method will return the value -1.
int pos = obj.indexOf("Ravi");
Code mentioned above will return the index (position) of the string "Ravi" in the list.
7) Object get(int index):This method will returns the object of list which is present at the specified index.
String str = obj.get(2);
Code mentioned above will return the string stored at 3rd position (index 2) and would be assigned to the string "str".
8) int size():This method will return size of the ArrayList(Number of elements of the list).
int number = obj.size();
9) boolean contains(Object obj):This method will check whether the given object obj is present in the array list.If object is there, it will return true else it will return false.
obj.contains("Mukesh");
Above code will return true if the string "Mukesh" is present in the list else it will return false.
10) clear():This method is used for removing all the elements of ArrayList. The below code will remove all the elements of ArrayList whose object is obj.
obj.clear();
Ways to iterate the elements of collection in java
- By Iterator interface
- By for-each loop
Iterating the elements of ArrayList by Iterator interface
package com.example; import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample { public static void main(String args[]) { // creating arraylist ArrayList<String> list = new ArrayList<String>(); // adding object in arraylist list.add("Ravi"); list.add("Mukesh"); list.add("Sonu"); list.add("Bala"); // getting Iterator from arraylist to traverse elements Iterator<String> itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
Output
Ravi Mukesh Sonu Bala
Iterating the elements of ArrayList by for-each loop
import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample { public static void main(String args[]) { // creating arraylist ArrayList<String> list = new ArrayList<String>(); // adding object in arraylist list.add("Ravi"); list.add("Mukesh"); list.add("Sonu"); list.add("Bala"); for (String str : list) System.out.println(str); } }
Output
Ravi Mukesh Sonu Bala
Related Articles
Related Articles