ArrayList is a part of collection framework and is present in java.util package
. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
NOTE: ArrayList is the BEST choice for retrieval operations while WORST choice for inserting values in some position of ArrayList
To convert a Collection
object into an ArrayList, it creates an ArrayList from the objects of the Collection
c
-
Appends the specified element to the end of this ArrayList.
-
Inserts the specified element at the specified position in this ArrayList.
ArrayList<String> al = new ArrayList<>(); al.add("Geeks"); al.add("Geeks"); al.add(1, "For"); System.out.println(al); // [Geeks, For, Geeks]
-
Appends all of the elements in the specified Collection to the end of this this ArrayList, in the order that they are returned by the specified Collection's Iterator.
-
Inserts all of the elements in the specified Collection into this ArrayList, starting at the specified position.
-
Removes all of the elements from this ArrayList.
-
Returns a shallow copy of this ArrayList.
-
Returns true if this ArrayList contains the specified element.
-
Searches for the first occurence of the given argument, testing for equality using the equals method.
-
Tests if this ArrayList has no components.
-
Returns the index of the last occurrence of the specified object in this ArrayList.
-
Removes the element at the specified position in this ArrayList.
ArrayList<String> al = new ArrayList<>(); al.add("Geeks"); al.add("Geeks"); al.add(1, "For"); System.out.println("Initial ArrayList " + al); //Initial ArrayList [Geeks, For, Geeks] al.remove(1); System.out.println("After the Index Removal " + al); //After the Index Removal [Geeks, Geeks] al.remove("Geeks"); System.out.println("After the Object Removal " + al); //After the Object Removal [Geeks]
-
Replaces the element at the specified position in this ArrayList with the specified element.
ArrayList<String> al = new ArrayList<>(); al.add("Geeks"); al.add("Geeks"); al.add(1, "Geeks"); System.out.println("Initial ArrayList " + al); //Initial ArrayList [Geeks, Geeks, Geeks] al.set(1, "For"); System.out.println("Updated ArrayList " + al); //Updated ArrayList [Geeks, For, Geeks]
-
Returns the element at the specified position in this ArrayList.
-
Returns the number of components in this ArrayList.
ArrayList<String> al = new ArrayList<>(); al.add("Geeks"); al.add("Geeks"); al.add(1, "For"); // Using the Get method and the // for loop for (int i = 0; i < al.size(); i++) System.out.print(al.get(i) + " "); System.out.println(); // Using the for each loop for (String str : al) System.out.print(str + " "); }