I want to learn java again. I hope I can get more things during the second learning.
Think back to the method of traversing every element of the container. I think most people will say it is a for loop. If it is an array, you can use the. length attribute to traverse it. In c ++, the array does not have this attribute. Both js and python are available. Now I will gradually learn how to use iterator, because it is very convenient.
With iterator, you can view and delete an element in a container, but cannot add one element.
First, you must obtain the iterator of a container. The method is simple,
Iterator it = container. itertor ();
Container is a container object. At this time, it does not point to any elements in the container, but is at the beginning of the container mouth. To access the elements, you must use the iterator method. There are three methods:
HasNext checks whether there are other elements
Next, return the object to which the cursor is directed.
Remove deletes the currently pointed Element
Note that the container can be any container type, including set, list, and map. Of course, map needs to first return a set of keywords through the keyset method, and then access each primary object by traversing each primary key.
The basic structure is as follows:
Iterator it = container. iterator ()
While (it. hasNext ()){
Use the next method of iterator to return objects and perform corresponding operations.
}