There are no empty slots. Learn 4 Techniques to PRINT ArrayList Elements in Java with Code Example. Most of the developers choose Arraylist over Array as it’s a very good alternative of traditional java arrays. Java ArrayList Printing Elements Please Use CODE Tags. Each element can be accessed using the parameter provided inside the forEach () function. If hasNext() returns true, the loop fetchs the ArrayList elements using Iterator next() method and print it using System.out.println mehtod. out. Process 2: Java provides forEach (); method for ArrayList. This method takes a parameter of int type and returns the element. Standard arrays in Java are fixed in the number of elements they can have. set( int index, E elt ) The element previously at index is replaced with the reference elt.The index should be in the range 0 to size-1.. An IndexOutOfBoundsException is thrown if the index is out of bounds. Another method next() of Iterator returns elements. What are ways to Prevent Inheritance in Java Programming? In the following example, we will initialize an ArrayList with some elements and print them using for loop. Setting ArrayList Elements. Using iterator. This example shows: 1. Process 1: Java For Loop can be used to iterate through all the elements of an ArrayList. ListIterator is similar to iterator with difference that iterator is unidirectional and ListIterator is bidirectional. The only thing in case of list iterator is that we have to initialize it with null at first. In this tutorial, we'll discuss how to create a multidimensional ArrayListin Java. Friday By BuckGup November 8 in Programming. remove(Object) –> to delete a particular element by specifying object name; clear() –> to delete all elements present in ArrayList; Note: Both above methods inherited from Collection interface . In the loop, we are checking if next element is available using hasNext() method. use for loop and get index values assigned to two separate variable then you can easily print public static void main(String[] args) { List list=new java.util.ArrayList(); list.add(2); list.add(4); list.add(7); list.add(9); list.add(10); int firstnumber = 0,secondnumber = 0; for(int i=0;i