Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.1k points)
Can anybody help me with printing arrayList using forEach() loop in Java?

1 Answer

0 votes
by (11.7k points)

ForEach() method does the traversing of each element of an iterable of array lists as long as all elements are processed by this method or an exception is raised. Whatever exception is thrown during operation is passed to the caller. There can be more than one exception.  

 

Snippet of Syntax: public void forEach(Employee<? super E> action)

 

It takes action as a parameter which represents the action to be performed for each element. It returns nothing. If the specified action is Null, it throws an NullPointerException. 

Code:

import java.util.*; 

public class GFG { 

public static void main(String[] args) 

// ArrayList is created which is going to 

// contain a list of Numbers 

ArrayList<Integer> Numbers = new ArrayList<Integer>(); 

// Add Number to list 

Numbers.add(24); 

Numbers.add(87); 

Numbers.add(78); 

Numbers.add(73); 

// forEach method of ArrayList and 

// print numbers 

Numbers.forEach((n) -> System.out.println(n)); 

If you want to become proficient in Java, check the Java tutorial offered by intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...