java.util.ArrayList:
ArrayList is also concrete sub-class of collection framework classes whose object allows us to organize the data either in similar type or in different type.
Creating ArrayList is nothing but creating an object of ArrayList class.
ArrayList API:
ArrayList (); ArrayList (int size);
Advantages of ArrayList over LinkedList:
ArrayList
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> country = new ArrayList(); country.add("India"); country.add("Pakistan"); country.add("China"); country.add("SriLanka"); country.add("Australia"); country.add("England"); country.add("Japan"); country.add("Bangladesh"); country.add("USA"); //Iterate ArrayList using for loop System.out.println("Using for loop"); for (int i = 0; i < country.size(); i++) { System.out.println(country.get(i)); } //Iterate ArrayList using advanced for loop System.out.println("Using Advanced for loop"); for (String name : country) { System.out.println(name); } //Iterate ArrayList using Iterator System.out.println("Using Iterator"); Iterator iterator = country.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } //Iterate ArrayList using wihle loop System.out.println("Using while loop"); int index = 0 ; while (country.size()> index) { System.out.println(country.get(index)); index++ ; } //Add element on particular index country.add(2, "Koria"); System.out.println("After adding new element on 1 index"); Iterator iteratorNew = country.iterator(); while (iteratorNew.hasNext()) { System.out.println(iteratorNew.next()); } //Add the one ArrayList data to another ArrayList List<String> newCountry = new ArrayList(); newCountry.add("SouthAfrica"); country.addAll(newCountry); System.out.println("After adding new ArrayList to country List"); Iterator iteratorNewCountry = country.iterator(); while (iteratorNewCountry.hasNext()) { System.out.println(iteratorNewCountry.next()); } //Clear all the element from ArrayList newCountry.clear(); System.out.println("After clearing newCountry List"); Iterator iteratorAfterClear = newCountry.iterator(); while (iteratorAfterClear.hasNext()) { System.out.println(iteratorAfterClear.next()); } //find the element in ArryList using contains() method if(country.contains("India")){ System.out.println("The ArrayList having country India"); } //find the index of a element in ArrayList int indexOfIndia = country.indexOf("India"); if(indexOfIndia != -1){ System.out.println("The index of India is:"+indexOfIndia); } //test the ArrayList whether it is empty or not? if(!country.isEmpty()){ System.out.println("Country list is not empty."); } //Sort the ArrayList Collections.sort(country); System.out.println("After sorted the order"); Iterator iteratorSort = country.iterator(); while (iteratorSort.hasNext()) { System.out.println(iteratorSort.next()); } //Remove the element form Array //Based on index country.remove(1); //Based on element country.remove("USA"); //remove the element by Collection List deletedList = new ArrayList(); deletedList.add("Pakistan"); country.removeAll(deletedList); System.out.println("After removing the elements"); Iterator iteratorRemove = country.iterator(); while (iteratorRemove.hasNext()) { System.out.println(iteratorRemove.next()); } //Retains only the elements in this list that are contained in the specified collection List retain = new ArrayList(); retain.add("Koria"); country.retainAll(retain); System.out.println("After retaining the elements"); Iterator iteratorRetain = country.iterator(); while (iteratorRetain.hasNext()) { System.out.println(iteratorRetain.next()); } } }
Output: