How to print a list using forEach in Java

2 Answers

0 votes
import java.util.*; 

public class MyClass {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
	    
	    list.add("java");
	    list.add("c");
	    list.add("c++");
	    list.add("c#");
	    list.add("php");

	    list.forEach(item->System.out.println(item));
    }
}



/*
run:

java
c
c++
c#
php

*/

 



answered Sep 29, 2019 by avibootz
0 votes
import java.util.*; 
 
public class MyClass {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
         
        list.add("java");
        list.add("c");
        list.add("c++");
        list.add("c#");
        list.add("php");
 
        list.forEach(System.out::println);
    }
}
 
 
 
/*
run:
 
java
c
c++
c#
php
 
*/

 



answered Sep 30, 2019 by avibootz

Related questions

1 answer 197 views
1 answer 204 views
1 answer 145 views
2 answers 250 views
1 answer 225 views
2 answers 257 views
...