Java Program to Iterate over an ArrayList
What is ArrayList in Java ?
In Java, an ArrayList is a class that provides a resizable array implementation of the List interface. It allows you to store and manipulate a collection of objects in a dynamic manner, with the ability to add, remove, and access elements by their index.
Unlike a regular array, an ArrayList can grow and shrink in size dynamically as elements are added or removed. This makes it a more flexible data structure that can adapt to changing needs.
More on ArrayList in Java :
ArrayLists can be used to store objects of any type, including primitive data types like int, double, etc. To add elements to an ArrayList, you can use the add() method, and to retrieve an element, you can use the get() method. You can also remove elements using the remove() method and iterate over the elements using a for loop or an enhanced for loop.
Steps to Iterate over an ArrayList :
- Create an ArrayList
- Add elements to the ArrayList using the add() method
- Get the size of the ArrayList using the size() method
- Iterate over the elements in the ArrayList using a for loop:
a. Get the current element using the get() method
b. Do something with the current element - End the loop
Here’s a pseudo-code of the algorithm :
1. ArrayList myList = new ArrayList 2. myList.add("element1") myList.add("element2") myList.add("element3") 3. int size = myList.size() 4. for i = 0 to size - 1 a. String currentElement = myList.get(i) b. // Do something with currentElement 5. end for
Inside the loop, we get the current element using the get() method and then do something with it. This could be printing the element to the console, manipulating it in some way, or anything else that your program needs to do.
When the loop is complete, we have iterated over all elements in the ArrayList.
Example 1 :
import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create a new ArrayList ArrayList< String> myList = new ArrayList<>(); // Add some elements to the ArrayList myList.add("prepinsta"); myList.add("prepinsta Prime"); myList.add("coding"); // Get the size of the ArrayList int size = myList.size(); // Iterate over the elements in the ArrayList using a for loop for (int i = 0; i < size; i++) { String currentElement = myList.get(i); System.out.println(currentElement); } } }
Output :
prepinsta prepinsta Prime coding
Example 2 :
import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create a new ArrayList ArrayList< String> myList = new ArrayList<>(); // Add some elements to the ArrayList myList.add("rishikesh"); myList.add("ayush"); myList.add("lakshit"); // Iterate over the elements in the ArrayList using an enhanced for loop for (String currentElement : myList) { System.out.println(currentElement); } } }
Output :
rishikesh ayush lakshit
Inside the loop, we declare a new variable called currentElement of type String, which represents the current element of the ArrayList. We then print the currentElement variable to the console using System.out.println().
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment