A, iteration Overview
1. What is an iterator?
In Java, there are a lot of data containers, these operations have a lot in common. Java uses iterators to provide a common user interface for a variety of containers. Such that its traversal of the containers isolated specific underlying implementation, achieve decoupling.
It defines three methods in interface Iterator:
2, an iterator
public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add("abc"); list.add("edf"); list.add("ghi"); for(Iterator<String> it=list.iterator();it.hasNext();) { System.out.println(it.next()); } }
Results of the:
Two, ArrayList realization of Iterator
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; ... }
ArrayList defined within an inner class Itr, which implements the Iterator interface.
In Itr, there are three variables
cursor: index indicates a position of the lower element
lastRet: index indicates a position of an element
expectModCount: the expected number of modified
The following look at class Itr implements Iterator interface of three methods:
public boolean hasNext() { ! Return cursor = size; // When the cursor is not equal to the size, that they had the element index }
public E next () // Returns next element { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData [lastRet = i]; }
A checkForComodification () method next () method, which is implemented as follows:
final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
You can see, this function is used to determine the number of times to modify the collection is legitimate.
Maintained within a set number of times for the recording field modCount collection is modified, time changes (add, remove, set) whenever the internal structure of the collection, modCount + 1.
Iterator within expectedModCount also maintains a field, set the same recording current frequency and a modified, initialized to the value set modCount. When we traverse of the call Iterator, if there are other threads modify the list will appear modCount! = ExpectedModCount circumstances, the concurrent modifications will be reported abnormal java.util.ConcurrentModificationException. The following sample code is:
public static void main(String[] args) { ArrayList<String> aList=new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); Iterator<String> it=aList.iterator(); while(it.hasNext()) { if("abc".equals(it.next())) { aList.remove("abc"); } } System.out.println ( "after removal:" + aList); }
The above code, if we only use iterators to delete does not occur concurrent modification exception error.
public static void main(String[] args) { ArrayList<String> aList=new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); Iterator<String> it=aList.iterator(); while(it.hasNext()) { if("abc".equals(it.next())) { it.remove(); } } System.out.println ( "after removal:" + aList); }
public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
While performing the remove operation, the same first execution checkForComodification (), and then executes remove ArrayList () method, which will modCount value plus 1, where we will expectedModCount = modCount, so that it remains unified.
三, ListIterator
Can be seen above, Iterator provides only way to remove an element remove, if we want to add elements in the traversal of how to do?
ListIterator interface extends Iterator interface, that allows programmers to modify the list to traverse, during iteration, and obtain the iterator's current position in the list according to either direction.
ListIterator interface defines the following several ways:
The following operations while adding elements to the list with the use ListIterator edge traversal:
public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); ListIterator <String> listIt = aList.listIterator (); while (listIt.hasNext()) { if ("abc".equals(listIt.next())) { listIt.add("haha"); } } System.out.println ( "after removal:" + aList); }
A, iteration Overview
1. What is an iterator?
In Java, there are a lot of data containers, these operations have a lot in common. Java uses iterators to provide a common user interface for a variety of containers. Such that its traversal of the containers isolated specific underlying implementation, achieve decoupling.
It defines three methods in interface Iterator:
2, an iterator
public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add("abc"); list.add("edf"); list.add("ghi"); for(Iterator<String> it=list.iterator();it.hasNext();) { System.out.println(it.next()); } }
Results of the:
Two, ArrayList realization of Iterator
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; ... }
ArrayList defined within an inner class Itr, which implements the Iterator interface.
In Itr, there are three variables
cursor: index indicates a position of the lower element
lastRet: index indicates a position of an element
expectModCount: the expected number of modified
The following look at class Itr implements Iterator interface of three methods:
public boolean hasNext() { ! Return cursor = size; // When the cursor is not equal to the size, that they had the element index }
public E next () // Returns next element { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData [lastRet = i]; }
A checkForComodification () method next () method, which is implemented as follows:
final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
You can see, this function is used to determine the number of times to modify the collection is legitimate.
Maintained within a set number of times for the recording field modCount collection is modified, time changes (add, remove, set) whenever the internal structure of the collection, modCount + 1.
Iterator within expectedModCount also maintains a field, set the same recording current frequency and a modified, initialized to the value set modCount. When we traverse of the call Iterator, if there are other threads modify the list will appear modCount! = ExpectedModCount circumstances, the concurrent modifications will be reported abnormal java.util.ConcurrentModificationException. The following sample code is:
public static void main(String[] args) { ArrayList<String> aList=new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); Iterator<String> it=aList.iterator(); while(it.hasNext()) { if("abc".equals(it.next())) { aList.remove("abc"); } } System.out.println ( "after removal:" + aList); }
The above code, if we only use iterators to delete does not occur concurrent modification exception error.
public static void main(String[] args) { ArrayList<String> aList=new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); Iterator<String> it=aList.iterator(); while(it.hasNext()) { if("abc".equals(it.next())) { it.remove(); } } System.out.println ( "after removal:" + aList); }
public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
While performing the remove operation, the same first execution checkForComodification (), and then executes remove ArrayList () method, which will modCount value plus 1, where we will expectedModCount = modCount, so that it remains unified.
三, ListIterator
Can be seen above, Iterator provides only way to remove an element remove, if we want to add elements in the traversal of how to do?
ListIterator interface extends Iterator interface, that allows programmers to modify the list to traverse, during iteration, and obtain the iterator's current position in the list according to either direction.
ListIterator interface defines the following several ways:
The following operations while adding elements to the list with the use ListIterator edge traversal:
public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("bbc"); aList.add("abc"); aList.add("ysc"); aList.add("saa"); System.out.println ( "Before removing:" + aList); ListIterator <String> listIt = aList.listIterator (); while (listIt.hasNext()) { if ("abc".equals(listIt.next())) { listIt.add("haha"); } } System.out.println ( "after removal:" + aList); }