How to Loop through Element of the ArrayList in Java


In this example, we’ll learn how to get size or number of elements currently stored in ArrayList. We will also see how to loop through element of the ArrayList.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class GetNumberArrayListIterateExample {

	public static void main(String[] args) {

		ArrayList al = new ArrayList();

		al.add("a");
		al.add("b");
		al.add("c");
		al.add("d");

		for (int i = 0; i < al.size(); i++) {
			// Gets the element of the specified index
			String element = al.get(i);
			System.out.println(element);
		}

	}

}

Output:

a
b
c
d

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments