Java print out list strings

Here Is 4 Ways To Print ArrayList Elements In Java

Learn 4 Techniques to PRINT ArrayList Elements in Java with Code Example.

To print elements, first we’ll create a String ArrayList and store weekdays name as strings into it and display them using following ways:

Here is a string ArrayList. We have created an object of the list and added name of week days using add() method of ArrayList.

ArrayList arrlist=new ArrayList(); arrlist.add("Sunday"); arrlist.add("Monday"); arrlist.add("Tuesday"); arrlist.add("Wednesday"); arrlist.add("Thursday"); arrlist.add("Friday"); arrlist.add("Saturday"); 

PRINTING ARRAYLIST

1) Using for loop

//using for loop System.out.println("Using For Loop\n "); for (int i = 0; i

To use for loop, we’re getting the length of the ArrayList using its size() method, up to which we need to retrieve elements. We have used get(i) method of the arraylist to fetch the data present at the indexes of the arraylist.

2) Using for-each loop

//using for-each loop System.out.println("\nUsing for-each loop\n"); for (String str : arrlist)

Here, the same for loop is written in another form using for each loop or advance loop method in java. This type of loop fetchs every elements from the arralist object one by one.

Читайте также:  Найти середину массива питон

FYI, MY BOOK FOR YOU

3) Using iterator

//using iterator System.out.println("\nUsing Iterator"); Iterator itr=arrlist.iterator(); while(itr.hasNext())

Iterators in java collection framework are used to retrieve elements one by one. ArrayList iterator() method returns an iterator for the list. The Iterator contains methods hasNext() that checks if next element is available. Another method next() of Iterator returns elements.

We have implemented while loop to traverse the ArrayList. In the loop, we are checking if next element is available using hasNext() method. Iterator hasNext() methods returns the Boolean value true or false. If hasNext() returns true, the loop fetchs the ArrayList elements using Iterator next() method and print it using System.out.println mehtod. If hasNext() returns false, there is no further elements in the list.

4) Using list-iterator

//Using list iterator litr=arrlist.listIterator(); System.out.println("\n Using list iterator"); while(litr.hasNext())

We can also use ListIterator to traverse elements of the ArrayList. ListIterator also has methods hasNext() to check if element is present and next() method to retrieve ArrayList elements. 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.

ListIterator litr = null;

Complete Code to print ArrayList Elements in Java using all 4 Ways

The complete code to print all elements of the ArrayList using all 4 techniques is given below:

import java.util.*; import java.util.ArrayList; import java.util.Iterator; public class Arrlist < public static void main(String[] args) < ListIterator litr = null; ArrayList arrlist=new ArrayList(); arrlist.add("Sunday"); arrlist.add("Monday"); arrlist.add("Tuesday"); arrlist.add("Wednesday"); arrlist.add("Thursday"); arrlist.add("Friday"); arrlist.add("Saturday"); //using for loop System.out.println("Using For Loop\n "); for (int i = 0; i < arrlist.size();i++) < System.out.println(arrlist.get(i)); >//using for-each loop System.out.println("\nUsing for-each loop\n"); for (String str : arrlist) < System.out.println(str); >//using iterator System.out.println("\nUsing Iterator"); Iterator itr=arrlist.iterator(); while(itr.hasNext()) < String obj = itr.next(); System.out.println(obj); >//Using list iterator litr=arrlist.listIterator(); System.out.println("\n Using list iterator"); while(litr.hasNext()) < System.out.println(litr.next()); >> > 

Output
Using For Loop

Using list iterator
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Users Question on ArrayList from comments:

Q – Suppose I have three or more arraylist . Do I need to write the code again and again to print the Elements ? – Himanshu

Answer: NO, you can write a single method to print multiple arraylists and call the method for each array list as shown below.

In the below code example, there are two array list “days” and “fruits” . The single method “printArrayListElements(ArrayList a)” is being used to print both the array lists.

import java.util.ArrayList; public class Sample < public static void main(String[] args) < ArrayList days = new ArrayList(); days.add("Sunday"); days.add("Monday"); days.add("Tuesday"); days.add("Wednesday"); days.add("Thursday"); days.add("Friday"); days.add("Saturday"); System.out.println("Days Array List:"); printArrayListElements(days); ArrayList fruits = new ArrayList(); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Orange"); System.out.println("Fruits Array List:"); // print fruits array list printArrayListElements(fruits); >public static void printArrayListElements(ArrayList a) < for (int i = 0; i < a.size(); i++) < System.out.println(a.get(i)); >> > 

Days Array List:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Fruits Array List:
Apple
Mango
Orange

Источник

How to Print a List in Java

List is a Java class that belongs to java.util package. It is inherited from the Collection interface and used to keep a collection in order. The List interface supports ArrayList and LinkedList. In Java programming, the ArrayList and LinkedList are frequently used, and the Java collection ArrayList implements the List.

This write-up will specifically discuss the method of printing a list in Java.

How to Print a List in Java?

To print a List in Java, you can use:

Let’s check out each of the mentioned methods one by one!

Method 1: Print a List in Java Using toString() Method

The “toString()” method can be used to print a list in Java. This method prints the specified list elements directly using their references.

Example
First, we will create a list of String type named “gadgetList” using the Java collection ArrayList:

Then, we will add the values to the list with the help of the “add()” method:

gadgetList. add ( «Mobile» ) ;
gadgetList. add ( «Computer» ) ;
gadgetList. add ( «Laptop» ) ;
gadgetList. add ( «Headphones» ) ;

Now, we will call the “toString()” method to print the list on the console with the help of “System.out.println()” method:

Want to perform the same operations using loops? Head towards the next sections!

Method 2: Print a List in Java Using for Loop

The most common method to print the List in Java is using a “for” loop. The for loop iterates over the list until its size and prints out the values using the “System.out.println()” method.

Example
In this example we consider the same list named “gadgetList” and print its values using “for” loop:

The given output indicates that we have successfully displayed the list values with the help of the “for” loop:

Method 3: Print a List in Java Using for-each Loop

The “for-each” loop can also be utilized for the same purpose. It contains a variable with the same type as the List, colon, and the list’s name. This loop saves elements in the loop variable later printed on the console.

Example
In this example, we print the list named “gadgetList” using a “for-each” loop. So, we will create a String type variable named “gadgets”. Then, print the elements of the list using the “System.out.println()” methods:

We have offered all of the essential methods related to printing a list in Java.

Conclusion

To print a list in java, you can use three approaches: for loop, for-each loop, and toString() method. The for loop iterates over the list until its size and prints out the values using the System.out.println() method. The for-each loop contains a variable the same type as the List, colon, and the list’s name and saves elements in the loop variable. Lastly, the toString() method directly prints the specified list elements using their references. This write-up discussed the methods for printing a list in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Print List in Java

  1. Print List in Java Using the Enhanced for Loop
  2. Print List in Java Using toString()
  3. Print List in Java Using forEach()

We will go through a few methods that can print out all the list items in Java. In the examples, we will use a model class to demonstrate how we can create a list of model objects and then print items in them.

To get all the elements from our Java list, we will create an enhanced loop that will loop through every item in the list and then print it out. In our case, a list item is a class object. Thus we have to call the method of every object to print list items.

import java.util.ArrayList; import java.util.List;  public class Main   public static void main(String[] args)    ListDummyModel> dummyModels = new ArrayList<>();   DummyModel dm1 = new DummyModel();  dm1.setName("John Doe");  DummyModel dm2 = new DummyModel();  dm2.setName("Sam Alex");  DummyModel dm3 = new DummyModel();  dm3.setName("Max Payne");  DummyModel dm4 = new DummyModel();  dm4.setName("Jp Cooper");  dummyModels.add(dm1);  dummyModels.add(dm2);  dummyModels.add(dm3);  dummyModels.add(dm4);   for (DummyModel model : dummyModels)   System.out.println(model.getName());  >   > >  class DummyModel    private String name;   public String getName()   return name;  >   public void setName(String name)   this.name = name;  > > 
John Doe Sam Alex Max Payne Jp Cooper 

We insert only String values into the model class, and to get the values back from the list, we can override the toString() and return the item through it. We will get an array of items.

import java.util.ArrayList; import java.util.Arrays; import java.util.List;  public class Main   public static void main(String[] args)    ListDummyModel> dummyModels = new ArrayList<>();   DummyModel dm1 = new DummyModel();  dm1.setName("John Doe");  DummyModel dm2 = new DummyModel();  dm2.setName("Sam Alex");  DummyModel dm3 = new DummyModel();  dm3.setName("Max Payne");  DummyModel dm4 = new DummyModel();  dm4.setName("Jp Cooper");  dummyModels.add(dm1);  dummyModels.add(dm2);  dummyModels.add(dm3);  dummyModels.add(dm4);   System.out.println(dummyModels.toString());   > >  class DummyModel    private String name;   public void setName(String name)   this.name = name;  >   @Override  public String toString()   return name;  > > 

Источник

Оцените статью