Ways to iterate over a list in Java

后端 未结 12 1822
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:46

Being somewhat new to the Java language I\'m trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or

12条回答
  •  有刺的猬
    2020-11-22 01:13

    In java 8 you can use List.forEach() method with lambda expression to iterate over a list.

    import java.util.ArrayList;
    import java.util.List;
    
    public class TestA {
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("Apple");
            list.add("Orange");
            list.add("Banana");
            list.forEach(
                    (name) -> {
                        System.out.println(name);
                    }
            );
        }
    }
    

提交回复
热议问题