Java ArrayList print all elements

In this post, we will see how to print ArrayList in java.

Java has Array, and it’s a very powerful data structure, but Array has its own set of limitations. The ArrayList data structure was introduced to overcome these limitations.
Before we proceed with our topic, let me quickly brief you with ArrayList.

ArrayList Introduction

ArrayList is an advanced version of Array, as it allows us to implement resizable arrays. Apart from this, ArrayList is a part of Java collections, and it implements Java List. Though ArrayList is the advanced version of array, ArrayList is less efficient than basic arrays in terms of time optimization.

Since we are not focusing on ArrayList Introduction, I have kept it straight and simple; if you are interested in learning more about ArrayList, you can refer ArrayList in java.

6 Ways to Print ArrayList in Java

Yes, you read it right! Fortunately, there isn’t a single way to print ArrayList elements. Though there are dozens of different ways, I’ll cover the three most popular ones in this guide.

Print ArrayList in java using for loop

In the for loop, we are iterating upto the size[] of Arraylist. In each iteration, using the get[] method of ArrayList, we are retrieving individual element. Finally, with the help of System.out.println statements, we will print those elements.

Have a look at the illustration:

package org.arpit.java2blog;

import java.util.ArrayList;

public class PrintArrayListMain {

    public static void main[String[] args] {

        ArrayList arl = new ArrayList[];

        System.out.println["Elements of ArrayList are:"];

        for [int i = 0; i

Chủ Đề