• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Print ArrayList in Java

July 31, 2023 //  by Talha Malik

Printing the code functionalities is vital in programming to analyze the code behavior from time to time. It is such that this analysis assists the programmer to apply checks on the code chunks in the case of complex implemented algorithms or the appended records. The ArrayList in Java is a container that can be appended with records and needs to be analyzed via printing in the case of added bulk values. 

How to Print ArrayList in Java?

To print an ArrayList in Java, apply the following approaches:

  • “for” and “for-Each” Loops.
  • “Iterator” Interface.
  • “Stream”.

Approach 1: Print an ArrayList Using the “for” and “for-Each” Loops

This approach demonstrates to print an ArrayList in the following code via the discussed loops:

import java.util.ArrayList;
public class PrintArraylist {
public static void main(String[] args) {
  ArrayList<Integer> items = new ArrayList<>();
  items.add(1);
  items.add(2);
  items.add(3);
  System.out.println("ArrayList Elements -> ");
  for (int i = 0; i < items.size(); i++) {
  System.out.println(items.get(i) + " ");
}
}}

In this code snippet:

  • Include the given package to access the “ArrayList” class.
  • In “main”, create an Integer ArrayList.
  • After that, insert the given integer values in it using the “add()” method.
  • Use the “for” loop to traverse the ArrayList elements and retrieve them.

Output

The ArrayList elements are traversed and printed appropriately. 

The following demonstration utilizes the “for-Each” loop instead to achieve the same functionality:

Approach 2: Print an ArrayList Using the “Iterator” Interface

The “Iterator” interface enables to invoke elements of a collection. In this approach, its following methods will be used:

  • Iterator(): It gives an iterator for the ArrayList.
  • hasNext(): This method applies a check on the ArrayList to analyze if the next element is contained or not.
  • next(): It retrieves the next element of ArrayList.

Example

The below-given code prints the ArrayList via the discussed methods:

import java.util.*;
import java.util.Iterator;
public class PrintArraylist {
public static void main(String args[]) {
  ArrayList<Integer> items = new ArrayList<>();
  items.add(1);
  items.add(2);
  items.add(3);
  System.out.println("ArrayList Elements -> ");
  Iterator<Integer> it = items.iterator();
  while(it.hasNext()){
  Integer x = it.next();
  System.out.println(x);
}
}}

According to the above block of code:

  • Import the specified package to work with the Iterator interface.
  • Now, likewise, create an integer ArrayList and add the provided values in it.
  • After that, apply the Iterator “iterator()” method to retrieve an iterator for the ArrayList.
  • Also, implement the “hasNext()” and “next()” methods to invoke the ArrayList elements if contained, thereby eliminating the limitations while iteration.

Output

This outcome signifies that the ArrayList elements are iterated accordingly.

However, the “ListIterator” interface can also be utilized to perform the same functionality, as follows:

Note: The “ListIterator” interface can also be considered for traversing the ArrayList in a reverse manner via its “hasPrevious()” and “previous()” methods.

Approach 3: Print an ArrayList Using Stream

The “Stream” approach can also be considered to print an ArrayList with the help of the “for-Each” loop:

import java.util.*;
public class PrintArraylist {
public static void main(String args[]) {
  ArrayList<Integer> items = new ArrayList<>();
  items.add(1);
  items.add(2);
  items.add(3);
  System.out.println("ArrayList Elements -> ");
  items.stream().forEach(System.out::println);
}}

In these code lines, similarly, define an integer ArrayList added with the specified integers via the “add()” method. After that, return a stream of the added integers (in the Arraylist) via the “for-Each” loop.

Output

Conclusion

To print an ArrayList in Java, apply the “for” or “for-Each” loops, the “Iterator” or “ListIterator” interfaces, or the “Stream” approach. All these approaches require traversing the ArrayList but the looping approach is quite efficient in printing the ArrayList directly based on its size.

Category: Java

Previous Post: « How to Call Method in Java
Next Post: How to Remove Duplicates From an Array in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact