StackTips

How To Loop ArrayList In Java

nilan avtar

Written by

Nilanchala,  6 min read,  2.66K views, updated on Sept. 17, 2023

In this tutorial we will see How To Loop ArrayList In Java in different ways. Below example will loop through ArrayList and print its content.

  1. Using regular for loop
  2. Using advance for loop
  3. Using While Loop
  4. Using Iterator

Iterator is an interface in the collection framework. ArrayList is a collection class and implements the List Inteface. All the elements of the ArrayList can be traversed by the Iterator. Iterator has methods hasNext() and next()

package com.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListDemo {

	public static void main(String[] args) {

		// adding items to list
		// in real time you might get this data from a db or from other sources
		// We are hard-coding the values here to simplify this example
		List list = new ArrayList();
		list.add(new NewsObject(1001,
				"Soldier beheaded in suspected terror attack in London",
				"en_in"));
		list.add(new NewsObject(1002,
				"Passive smoking can make kids aggressive", "en_in"));
		list.add(new NewsObject(
				1003,
				"Police prepare to question BCCI chief's son-in-law for betting links",
				"en_in"));
		list.add(new NewsObject(1004,
				"Shun body tattoos if you wish to join armed forces", "en_in"));
		list.add(new NewsObject(1005,
				"Man linked to Boston bombings killed by FBI agent", "en_in"));

		System.out.println("#1. Looping using regular for loop");
		for (int i = 0; i < list.size(); i++) {
			NewsObject news = (NewsObject) list.get(i);
			System.out.println(news);
		}

		System.out.println("#2.  Looping using advance for loop");
		for (NewsObject news : list) {
			System.out.println(news);
		}

		System.out.println("#3. Looping using while loop");
		int j = 0; // initilisation
		while (list.size() > j) { // condition checking
			NewsObject news = (NewsObject) list.get(j);
			System.out.println(news);
			j++; // iteration
		}

		System.out.println("#4. Looping using iterator");
		Iterator iterator = (Iterator) list.iterator();
		while (iterator.hasNext()) {
			NewsObject news = (NewsObject) iterator.next();
			System.out.println(news);
		}
	}

}

Here in my above example, I am storing NewsObject object in my ArrayList.

public class NewsObject {
	int id;
	String headline;
	String edition;

	NewsObject(int id, String headline, String edition) {
		this.id = id;
		this.headline = headline;
		this.edition = edition;

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getHeadline() {
		return headline;
	}

	public void setHeadline(String headline) {
		this.headline = headline;
	}

	public String getEdition() {
		return edition;
	}

	public void setEdition(String edition) {
		this.edition = edition;
	}

	@Override
	public String toString() {
		return "NewsObject : " + "Id = " + id + ", Headline=" + headline
				+ ", Editon=" + edition;
	}

}

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!