All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Print Arraylist In Java

Last Updated : Mar 11, 2024

How To Print Arraylist In Java

In this article we will show you the solution of how to print arraylist in java, as usual first we need to import package in case you are using eclipse then all program will import by itself during creation.

Second imported package ‘util.ArrayList’ must to get access of array list then you can implement inside coding.

Here we created arraylist and added values into arraylist then displayed each values through for loop help.

Step By Step Guide On How To Print Arraylist In Java :-

In main class ‘ArrLst_prgm’, we provide main method definition. There you need to create arraylist as per predefined rule and we inserting values one by one with the help of add() method.

Then to print array related values as usual you need loop help. For that reason, you used for loop and collecting values by get(). At last we printing result of each single value at terminal.

//Print Array List
package java_prgms;
import java.util.ArrayList;
class ArrLst_prgm
{
 public static void main(String args[])
    {
        ArrayList<Integer> Arr=new ArrayList<>();
        Arr.add(12);
        Arr.add(9);
        Arr.add(3);
        for(int i=0;i<Arr.size();i++){
         int cur=Arr.get(i);
         System.out.println(cur);
        }
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. Remember to print ArrayList package that will only make you to get error-free output instantly. Otherwise machine cannot understand the code related arraylist.
  3. In main class ‘ArrLst_prgm’, you need to create arraylist like we shown example or else you will get error. For successful ArrayList creation follow the syntax: ArrayList<DataType> Arraylist_Name=new ArrayList<>();.
  4. When you looking to insert values into array list you have to append ‘add()’ funcition with preferred values as per your wish.
  5. Here, we passing values ’12,9,3’ into arraylist, after that we defining for loop with arraylist length as condition. When it meets arraylist length it will comes out of loop.
  6. Within for loop, to collect iterating values from arraylist by get() function. Here you need to pass iteration variable to store that value to variable ‘cur’.
  7. Finally we displayed result at terminal by println() method. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to display arraylist in java. To compile java program you need to tell the java compiler which program to compile through ‘javac ArrLst_prgm.java’.

After successful compilation you need to execute ‘java ArrLst_prgm‘ line for gets the output.

Then you can see output '12, 9, 3’. On eclipse just press of run button then you will get output inside terminal. You can make changes on add() function to tests with new values.

I hope this article on how to print arraylist in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪