21 Jul Java ArrayList
The ArrayList is a Collection class and a part of the java.util package i.e., import the java.util.ArrayList package. It implements the List interface. The size of an ArrayList can be modified i.e., you can easily add or remove elements. This is not the case with Arrays in Java. Once created, you cannot modify the Java Arrays. ArrayList can have duplicate elements.
Let us see some ArrayList methods/ operations with examples:
- Create an ArrayList
- Size of an ArrayList
- Loop through the ArrayList
- Sort an ArrayList
- Access an ArrayList element
- Remove an ArrayList element
Let us first see how to create an ArrayList in Java:
Create an ArrayList
To create an ArrayList, set the type in angular braces i.e., use the wrapper class. For example, for creating an ArrayList with string elements, use <String>:
1 2 3 |
ArrayList<String> myArrList = new ArrayList<String>(); |
Let us now see an example and create an ArrayList in Java with string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println(sports); } } |
Output
1 2 3 |
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball] |
Size of an ArrayList
To get the size of an ArrayList i.e., how many elements it has, use the size() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println(sports); // Display the ArrayList size System.out.println("Count of elements = "+sports.size()); } } |
Output
1 2 3 4 |
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball] Count of elements = 6 |
Loop through the ArrayList
Use the for loop to loop through the ArrayList in Java. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println(sports); // Loop through the ArrayList for (int i = 0; i < sports.size(); i++) { System.out.println("Element "+(i+1)+" = "+sports.get(i)); } } } |
Output
1 2 3 4 5 6 7 8 9 |
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball] Element 1 = Cricket Element 2 = Football Element 3 = Tennis Element 4 = Badminton Element 5 = Basketball Element 6 = Volleyball |
Sort an ArrayList
To sort an ArrayList in Java, use the Collections.sort() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.ArrayList; import java.util.Collections; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println(sports); // Sort the ArrayList Collections.sort(sports); System.out.println("\nSorted ArrayList..."); // Loop through the sorted ArrayList for (int i = 0; i < sports.size(); i++) { System.out.println("Element "+(i+1)+" = "+sports.get(i)); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball] Sorted ArrayList... Element 1 = Badminton Element 2 = Basketball Element 3 = Cricket Element 4 = Football Element 5 = Tennis Element 6 = Volleyball |
Access an ArrayList element
To access an element of an ArrayList, use the get() method. Set the index number of the element you want to access as a parameter of the get() method. For example, to access the 3rd element, set index 2 i.e.:
1 2 3 |
sports.get(2) |
To access the 5th element, set index 4 i.e.:
1 2 3 |
sports.get(4) |
Let us now see an example to access an element of an ArrayList in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.ArrayList; import java.util.Collections; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println(sports); // Access elements System.out.println("Element 1 = "+sports.get(0)); System.out.println("Element 2 = "+sports.get(1)); System.out.println("Element 3 = "+sports.get(2)); } } |
Output
1 2 3 4 5 6 |
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball] Element 1 = Cricket Element 2 = Football Element 3 = Tennis |
Remove an ArrayList element
To remove an element from an ArrayList, use the remove() method. Set the index number of the element you want to remove as a parameter of the remove () method. For example, to remove the 3rd element, set index 2 i.e.:
1 2 3 |
sports.remove(2) |
To access the 5th element, set index 4 i.e.:
1 2 3 |
sports.remove(4) |
Let us now see an example of removing elements from an ArrayList in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.ArrayList; import java.util.Collections; class Studyopedia { public static void main(String[] args) { // Create an ArrayList with string elements ArrayList<String> sports = new ArrayList<String>(); // Adding string elements sports.add("Cricket"); sports.add("Football"); sports.add("Tennis"); sports.add("Badminton"); sports.add("Basketball"); sports.add("Volleyball"); // Display the ArrayList System.out.println("ArrayList = "+sports); // Remove two elements sports.remove(0); sports.remove(1); System.out.println("Updated ArrayList = "+sports); } } |
Output
1 2 3 4 |
ArrayList = [Cricket, Football, Tennis, Badminton, Basketball, Volleyball] Updated ArrayList = [Football, Badminton, Basketball, Volleyball] |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments