ArrayList In Java with Examples
Earlier we have seen Arrays and we found few limitations of Arrays in Java. Size of an Array is fixed.
Using ArrayList we can overcome the size issue. ArrayList is a resizable array.
ArrayList Class implements List interface. ArrayList allows duplicate elements (remember that Set in Java wont allow duplicate values. We will see Set in later sections)
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ ArrayList arrList = new ArrayList(); // We can use any data type arrList.add(987654321); // Here i am passing next two values as "STM" just to show you that List allows duplicate values.. // Set wont allow duplicate values. // Adding elements to the array list arrList.add("STM"); arrList.add("STM"); arrList.add(99.99); // To get all the values from the list System.out.println(arrList); // To get a value whose index is 1 System.out.println(arrList.get(1)); } }
Output:
[987654321, STM, STM, 99.99] STM
ArrayList using For loop:
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ ArrayList arrList = new ArrayList(); // Adding elements to the array list arrList.add(987654321); arrList.add("STM"); arrList.add("STM"); arrList.add(99.99); int listSize = arrList.size(); for(int i=0; i<listSize; i++){ System.out.println("Value of Array List at the index "+i+" is "+arrList.get(i)); } } }
Output:
Value of Array List at the index 0 is 987654321 Value of Array List at the index 1 is STM Value of Array List at the index 2 is STM Value of Array List at the index 3 is 99.99
ArrayList with For Each Loop:
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ ArrayList arrList = new ArrayList(); // Adding elements to the array list arrList.add(987654321); arrList.add("STM"); arrList.add(99.99); for(Object arrListItem : arrList){ System.out.println("Value of Array List at the index "+arrList.indexOf(arrListItem)+" is "+arrListItem); } } }
Output:
Value of Array List at the index 0 is 987654321 Value of Array List at the index 1 is STM Value of Array List at the index 2 is 99.99
ArrayList using Iterator:
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ ArrayList arrList = new ArrayList(); // Adding elements to the array list arrList.add(987654321); arrList.add("STM"); arrList.add(99.99); Iterator itr = arrList.iterator(); while(itr.hasNext()){ Object value=itr.next(); System.out.println("Values are "+value); } } }
Output:
Values are 987654321 Values are STM Values are 99.99
Creation of ArrayList with String elements:
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ // Creation of ArrayList with String elements ArrayList <String> arrList = new ArrayList<String>(); // Adding elements to the array list arrList.add("STM"); arrList.add("Software Testing Material"); for(String arrListItem:arrList){ System.out.println(arrListItem); } } }
Output:
STM Software Testing Material
Adding elements at a given index using ArrayList:
package classFiveArrays; import java.util.ArrayList; import java.util.Iterator; public class ArrayListClass { public static void main(String [] args){ // How to add elements at a given index using ArrayList // Creation of ArrayList with String elements ArrayList <String> arrList = new ArrayList<String>(); // Adding elements to the array list arrList.add(0, "STM"); arrList.add(1, "Software Testing Material"); System.out.println(arrList); //Remove elements from array list like this arrList.remove("Software Testing Material"); System.out.println(arrList); arrList.add(1, "Software Testing Material"); System.out.println(arrList); arrList.remove(1); System.out.println(arrList); } }
Output:
[STM, Software Testing Material] [STM] [STM, Software Testing Material] [STM]
Must Read: Java Tutorial