Java Program To List All Elements In List

Chapter: Miscellaneous Last Updated: 30-04-2023 19:15:30 UTC


            /* ............... START ............... */
import java.util.ArrayList;
import java.util.List;

public class ListElements {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<Integer>();

        for (int element : myList) {

                /* ............... END ............... */




  • First, the program creates an ArrayList called myList that contains five integers: 1, 2, 3, 4, and 5. The ArrayList class is part of the Java Collections Framework and provides dynamic arrays that can be resized as needed.
  • Next, the program uses a for loop to iterate over each element in myList. The loop declares a variable called element of type int, which will be used to store the value of each element in the list.
  • For each element in the list, the loop assigns its value to the element variable. Then, the println method of the System.out object is called to print the value of element to the console.
  • As a result, when you run the program, you'll see each element in the list printed on a separate line in the console.
  • Note that this program uses the enhanced for loop syntax, which was introduced in Java 5 and makes it easier to iterate over collections such as lists. It's also worth noting that the List interface in Java is a more general interface for lists, but ArrayList is a common implementation of this interface.


Java Program To List All Elements In List #How to print out all the elements of a List in Java #How to display all elements in list in Java

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018