[JAVA] Display all in Iterator by iterative processing by while in ArrayList array

How to display all Arraylist with Iterator


        //List declaration
        List<String> fruits =new ArrayList<>();

        fruits.add("Apple");
        fruits.add("watermelon");
        fruits.add("Pear");
        fruits.add("banana");

       //Iterator<Stromg>Declaration of
    Iterator<String> it2= fruits.iterator();
        
        //Use hasNext to continue the loop if there is a value
        while(it2.hasNext()){
            //Use next to get the value
            String nation = it2.next();
            System.out.println(nation);
        }

Recommended Posts

Display all in Iterator by iterative processing by while in ArrayList array
Addition of variables in iterative processing in a while statement
Numbers cannot be obtained by division in iterative processing using for
[Java] Various methods to acquire the value stored in List by iterative processing