Java

How to Print ArrayList Elements in Java

ArrayList is a class in java.util package and a component of the Java collection system. In Java, it offers us dynamic arrays. Although it could be slower than conventional arrays, dynamic arrays are useful in programs that require a lot of array manipulation. More specifically, the java.util package contains an “ArrayList” class. Unlike when declaring an array, where the size must be specified, an array list allows you to specify the size if you like.

This tutorial will explain printing ArrayList elements in Java.

How to Print ArrayList Elements in Java?

To print ArrayList elements in Java, there are multiple methods available, such as using “for loop”, “while loop”, “iterator” and many others.

Method 1: Print ArrayList Elements in Java Using the “for” Loop

To print ArrayList elements in Java using the “for” loop, first of all, import the “ArrayList” library of Java. To do so, the “java.util.ArrayList” is utilized:

import java.util.ArrayList;

After doing so:

  • Initialize an int variable with the particular value.
  • Next, create an integer array list with the help of the “new ArrayList<Integer>(n)” constructor.
  • Utilize the “for” loop and specify the required condition.
  • Add elements to the list with the help of the “add()” method.
  • System.out.println()” method is used for displaying the output on the console:
int n = 10;ArrayList<Integer> arrli= new ArrayList<Integer>(n);
for (int i = 15; i >= n; i--)

arrli.add(i);
System.out.println(arrli);

The “remove()” method is utilized for removing a specific number from the ArrayList:

arrli.remove(3);
System.out.println(arrli);

Output

Method 2: Print ArrayList Elements in Java Using “Iterator”

To print the ArrayList elements in Java, the “.iterator()” method can be utilized. To do so, follow the given instructions listed below:

  • First, initialize the string ArrayList string with the help of “ArrayList<String>()”.
  • Then, add the elements in a list by using the “add()” method.
  • System.out.println()” utilized for printing the list.
  • Iterator<String>” is a type that gives the collection’s iterator interface and encapsulates its iteration state.
  • iterator()” is used for returning a list iterator that will over the elements in this list.
  • hasNext()” method is utilized for checking if the Scanner has another token in its input.
  • next()” method of the ListIterator interface returns the next element in the specified list:
ArrayList<String> alist=new ArrayList<String>();

alist.add("Moon");
alist.add("Sun");

alist.add("Stars");
alist.add("Planets");
System.out.println("\nUsing Iterator");
Iterator<String> itr=alist.iterator();
while(itr.hasNext())

 {
 String obj = (String) itr.next();
 System.out.println(obj);
 }

Output

That’s all about printing the ArrayList element in Java.

Conclusion

To print ArrayList elements in Java, various methods can be utilized, including “for loop”, “while loop”, “iterator”, and many others. In this case, first, import the “ArrayList” library of Java. Then, add elements in the list with “add()” and use the “.iterator()” method. This post has explained the method for printing the ArrayList elements in Java.

About the author

Hafsa Javed