A set of iterations:
the elements of a set made of a traverse taken out.
Second iterator object
Iterator: iterator object, the iteration only from the top.
Boolean the hasNext (); if there is a next element determines the current pointer
Object next (): Get the next element pointer, and moves the pointer.
The ListIterator : is the sub-interface iterator interface, support bi-directional iteration, iteration from the top down, bottom-up iterations.
Enumeration: ancient iterator object, has now been replaced by the Vector iterator applies to the old class.
public class IteratorDemo {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
//方式一:使用for循环
for (int index=0;index<list.size();index++){
Object ele = list.get(index);
System.out.println(ele);
}
//方式二:使用foreach循环
for (Object ele : list) {
System.out.println(ele);
}
//方式三:使用while操作iterator迭代器
Iterator<Object> it = list.iterator();//得到迭代器对象
while (it.hasNext()){
Object ele = it.next();
System.out.println(ele);
}
//方式四:使用for循环来操作迭代器
for(Iterator<Object> it1 = list.iterator();it1.hasNext();){
Object ele = it1.next();
System.out.println(ele);
}
//方式五:使用古老的迭代器Enumeration(操作vector),现在基本不使用
Vector<Object> v = new Vector<>();
v.add("A");
v.add("B");
v.add("C");
Enumeration<Object> e = v.elements();
while (e.hasMoreElements()){
Object ele = e.nextElement();
System.out.println(ele);
}
}
}
Pointer iterators:
Three-depth analysis and foreach iterator:
. 1): foreach array can operate: the bottom layer is still used for loop index + acquired array element.
2): foreach Iterable instance may operate: in fact, used in the underlying . iterator (iterators)
directly using foreach and iterator collection element array can be simple.
decompile:
Four side edge iteration delete elements in a collection of problems:
when you need to edge iterator collection element, while deleting the specified element: this only use an iterator.
and can only use the remove method iterator object.
public class IteratorDemo1 {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
/*for (Object ele : list) {
if("C".equals(ele)){
list.remove(ele);//ConcurrentModificationException并发修改异常
}
System.out.println(ele);
}*/
//使用迭代器的删除方法
Iterator<Object> it = list.iterator();
while (it.hasNext()){
if("C".equals(it.next())){
it.remove();
}
}
System.out.println(list);//[A, B]
}
}
Principles and solutions:
it iterates the collection, while the iterative edge deletion is a very common operation:
how to solve the concurrent modification of abnormal it?
Do not use the Delete method of collection of objects.
In the Collection interface exists delete the specified element method: boolean remove ( Object ele);
this method can only be removed from the collection element, not the element specified in the iterator also be deleted .
Wang is: use the remove method of iterator.
this method removes the element is removed simultaneously from two threads . ensure the synchronization of the two threads.