Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to iterate over a list in Java

4 Answers

0 votes
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = Arrays.asList("java", "c", "c++", "python");
        
        for (int i = 0; i < list.size(); i++) {
            String element = list.get(i);
            System.out.println(element);
        }
    }
}




/*
run:

java
c
c++
python

*/

 



answered Nov 23, 2023 by avibootz
0 votes
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = Arrays.asList("java", "c", "c++", "python");
        
        for (String element : list) {
            System.out.println(element);
        }
    }
}




/*
run:

java
c
c++
python

*/

 



answered Nov 23, 2023 by avibootz
0 votes
import java.util.Iterator;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = Arrays.asList("java", "c", "c++", "python");
        
        Iterator<String> iterator = list.iterator();
        
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
        }
    }
}




/*
run:

java
c
c++
python

*/

 



answered Nov 23, 2023 by avibootz
0 votes
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = Arrays.asList("java", "c", "c++", "python");
        
        list.stream()
            .forEach(element -> {
                    System.out.println(element);
                });
    }
}




/*
run:

java
c
c++
python

*/

 



answered Nov 23, 2023 by avibootz

Related questions

2 answers 173 views
1 answer 118 views
4 answers 165 views
165 views asked Mar 16, 2023 by avibootz
1 answer 124 views
2 answers 105 views
105 views asked Mar 15, 2023 by avibootz
2 answers 117 views
117 views asked Mar 15, 2023 by avibootz
...