Ways to iterate over a list in Java

后端 未结 12 1846
孤独总比滥情好
孤独总比滥情好 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:00

    Above you'll find all differents ways to iterate over a LIST.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;
    
    public class test1 {
    
    public static void main(String[] args) {
        //******* Exercise 1 : Write a Java program to create a new array list, add some colors (string) and print out the collection.
        List colors = new ArrayList();
        colors.add("Black");
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        System.out.println(colors);
        
        
        //******* Exercise 2 : Write a Java program to iterate through all elements in a array list. 
        System.out.println("//******* Exercise 2");
        List list2 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        
        // iteration type 1 : using FOR loop
        System.out.println("// iteration type 1");
        for(Integer nb : list2) {
            System.out.print(nb + ", ");
        }
        System.out.println("\n");
        
        // iteration type 2 : using FOR loop
        System.out.println("// iteration type 2");
        for(int i=0; i < list2.size(); i++) {
            System.out.print(list2.get(i) + ", ");
        }System.out.println("\n");
        
        // iteration type 3  : using Do-While loop
        System.out.println("// iteration type 3");
        int index21 = 0;
        
        do {
            System.out.print(list2.get(index21) + ", ");
            index21++;
        }while(index21 {
             System.out.print(elt + ", ");
         });
    
        System.out.println("\n");
        
        
        // iteration type 6  : using  Iterator
        System.out.println("// iteration type 6");
        Iterator listIterator = list2.iterator();
        while(listIterator.hasNext()) {
            System.out.print( listIterator.next() + ", ");
        }
        
        System.out.println("\n");
        
        // iteration type 7  : using  Iterator (From the beginning)
        System.out.println("// iteration type 7");
        ListIterator listIterator21 = list2.listIterator(list2.size());
        while(listIterator21.hasPrevious()) {
            System.out.print( listIterator21.previous() + ", ");
        }
    
        System.out.println("\n");   
        
        // iteration type 8  : using  Iterator (From the End)
        System.out.println("// iteration type 8");
        ListIterator listIterator22 = list2.listIterator();
        while(listIterator22.hasNext()) {
            System.out.print( listIterator22.next() + ", ");
        }
    
        System.out.println("\n");   
    }
    
    }
    

提交回复
热议问题