The whole collection thing

1. Why do we use iterators for iterators?

First: Let’s do an example

public class Iterator1 {

      public static void main(String[] args) {
          / / set the list:

          ArrayList arrayList=new ArrayList();
            arrayList.add("1");
            arrayList.add("c");
            arrayList.add("33");
            arrayList.add("cc");

            //arrayList forEach method;
          arrayList.forEach(System.out::println);


           Foreach - enhanced for loop (name of data type traversal: traversal collection)
          for (Object oo:arrayList)
               {
              System.out.println(oo);
          }



         // Method 3: get the iterator print
            // Get the iterator print; ListIterator ()- starts with an object that gets to the iterator

          ListIterator listIterator = arrayList.listIterator();
          while (listIterator.hasNext()){

              // The iterator represents a collection of data directly. An iterator object can only be a single object.Object object = listIterator.next(); System.out.println(object); }}}Copy the code

This is how simple iterators can be used: – > will be set as a bus, one of the passengers is data one by one, if I want to iterate through each data, or to find a passenger, let go find the conductor (iterator iterator), it will not expose the = = = = the internal structure of the data and improve the security = = = =

The benefits of iterators (and why we use them)

Advantages: 1. You can directly traverse 2 without knowing the data structure inside the set. Do not expose the internal data, can directly external traversal; 3. Strong applicability, basically all sets can use iterators;Copy the code