ArrayList ========= Methods: ======== 1) add(): ======== -> can use to add an element to the array list at the end (appending). Syntax: ArrayListObject.add(element); -> add() can also use to add element at the specified position of an arraylist (Insertion). Syntax: ArrayListObject.add(position, element); -> if the specified index is immediate to the given size of arraylist: insertion is possible. -> if the specified index is exceeding the size of an arraylist: insertion is not possible. 2) size(): ========= ==> to get the total number of elements of an arraylist (length of the arraylist), we can use size() method. Syntax: ArrayListObject.size(); 3) remove(): =========== ==> based on the index/position to remove an element from an arraylist, we can use "remove()". Syntax: ArrayListObject.remove(position); -> remove() can accept an index which must be from the defined range of index from an arraylist. 4) addAll(): =========== -> can use to add more than one element to the array list object. Syntax: ArrayListObject.addAll(Another-Object); package io.arraylist; import java.util.ArrayList; public class ArrayListMainClass { public static void main(String[] args) { ArrayList cars = new ArrayList<>(); // adding elements into car object cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); ArrayList brands = new ArrayList<>(); brands.add("Amazon"); brands.add("Samsung"); brands.add("Facebook"); System.out.println("Cars = "+cars); System.out.println("Brands = "+brands); brands.addAll(cars); System.out.println("Brands = "+brands); } } =========================================================== 5) clear(): ========== -> can use to remove all elements from on ArrayList Object. Syntax: ArrayListObject.clear(); package io.arraylist; import java.util.ArrayList; public class ArrayListMainClass { public static void main(String[] args) { ArrayList cars = new ArrayList<>(); // adding elements into car object cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); System.out.println("Cars = "+cars); // cars.remove(0); cars.clear(); System.out.println(cars); } } ============================================================== 6) contains(): ============== -> to check the membership of the specified element in the given arraylist, we can use: contains() method. Syntax: ArrayListObject.contains(element); returns: true ==> if the specified element is in the given arraylist object. false ==> otherwise. package io.arraylist; import java.util.ArrayList; public class ArrayListMainClass { public static void main(String[] args) { ArrayList cars = new ArrayList<>(); // adding elements into car object cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); System.out.println("Cars = "+cars); System.out.println(cars.contains("Apple")); System.out.println(cars.contains("BMW")); } } ============================================================== isEmpty(): ========= -> to check whether the arraylist object is empty or not, we can use "isEmpty()" method. return: true ==> if it is empty false ==> otherwise package io.arraylist; import java.util.ArrayList; public class ArrayListMainClass { public static void main(String[] args) { ArrayList cars = new ArrayList<>(); // adding elements into car object cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); System.out.println("Cars = "+cars); System.out.println(cars.isEmpty()); } } ============================================= iterator(): ========== -> iterator() can use to create an iterator object. -> the iterator object can use to traverse on arraylist object element by element. -> at the time of object creation, the iterator object can point to before the 0th element of an arraylist by default. -> Syntax for the iterator object creation: Iterator iterator-object-name = arraylist-object.iterator(); -> to check whether the next position is with element or not, we can use "hasNext()" hasNext() return: true ==> next position with an element false ==> next position without an element -> if the next position is with an element, we need to change the position of the iterator object, we can use "next()". package io.arraylist; import java.util.ArrayList; import java.util.Iterator; public class ArrayListObjects { public static void main(String[] args) { // Creating of integer array list ArrayList lst = new ArrayList<>(); lst.add(100); lst.add(200); lst.add(300); lst.add(400); lst.add(500); System.out.println("The Given Array List Object = "+lst); Iterator it = lst.iterator(); while(it.hasNext()) { Integer i = it.next(); System.out.println(i); } } } ====================================================== for each loop: ============= package io.arraylist; import java.util.ArrayList; import java.util.Iterator; public class ArrayListObjects { public static void main(String[] args) { // Creating of integer array list ArrayList lst = new ArrayList<>(); lst.add(100); lst.add(200); lst.add(300); lst.add(400); lst.add(500); System.out.println("The Given Array List Object = "); for(Integer i:lst) { System.out.println(i); } } }