专业的JAVA编程教程与资源

网站首页 > java教程 正文

边玩手机边学Java---Java基础之Iterator

temp10 2024-09-14 12:57:01 java教程 9 ℃ 0 评论

1 迭代器

1.1 Iterator的API

public interface Iterator<E> {

边玩手机边学Java---Java基础之Iterator

/*** Returns {@code true} if the iteration has more elements.*/

boolean hasNext();

/*** Returns the next element in the iteration. */

E next();

/**

* Removes from the underlying collection the last element returned

* by this iterator (optional operation). This method can be called

* only once per call to {@link #next}. The behavior of an iterator

* is unspecified if the underlying collection is modified while the

* iteration is in progress in any way other than by calling this

* method.

*/

default void remove() {

throw new UnsupportedOperationException("remove");

}

1.2 ArrayList的iterator()方法

public Iterator<E> iterator() {

return listIterator();

}

public ListIterator<E> listIterator() {

return listIterator(0);

}

public ListIterator<E> listIterator(final int index) {

rangeCheckForAdd(index);

return new ListItr(index);

}

private class Itr implements Iterator<E> {

/**Index of element to be returned by subsequent call to next. */

int cursor = 0;

/** * Index of element returned by most recent call to next or

* previous. Reset to -1 if this element is deleted by a call

* to remove. */

int lastRet = -1;

/** * The modCount value that the iterator believes that the backing

* List should have. If this expectation is violated, the iterator

* has detected concurrent modification.

*/

int expectedModCount = modCount;

public boolean hasNext() {

return cursor != size();重要,放回是否有未被遍历的元素

}

public E next() {

checkForComodification();

try {

int i = cursor;

E next = get(i);

lastRet = i;

cursor = i + 1; 重要,游标后移

return next;

} catch (IndexOutOfBoundsException e) {

checkForComodification();

throw new NoSuchElementException();

}

}

public void remove() {

if (lastRet < 0)

throw new IllegalStateException();

checkForComodification();

try {

AbstractList.this.remove(lastRet);重要,移除上一个

if (lastRet < cursor) next后连续调用2次remove?

cursor--;

lastRet = -1;

expectedModCount = modCount;

} catch (IndexOutOfBoundsException e) {

throw new ConcurrentModificationException(); 重要

}

}

final void checkForComodification() {

if (modCount != expectedModCount)

重要,并发修改异常

throw new ConcurrentModificationException();

}

}

1.3 遍历List

public static void main(String[] args) {

List list = new ArrayList();

list.add("aaa");

list.add("bbb");

list.add("ccc");

//通过索引遍历List

for(int i=0;i<list.size();i++){

System.out.println(list.get(i));

}

//通过迭代器遍历List

for(Iterator iter2 = list.iterator();iter2.hasNext();){

String str = (String) iter2.next();

System.out.println(str);

iter2.remove();

iter2.remove();

}

Set set = new HashSet();

set.add("高1");

set.add("高2");

set.add("高3");

//通过迭代器遍历Set

// Iterator iter = set.iterator();

// while(iter.hasNext()){

for(Iterator iter = set.iterator();iter.hasNext();){

String str = (String) iter.next();

System.out.println(str);

}

1.4 遍历Map

public static void main(String[] args) {

Map map = new HashMap();

map.put("aa", "aaaa");

map.put("bb", "bbbb");

//遍历Map的第一种方式,先获取所有的键,根据键找值

Set keys = map.keySet();

for(Iterator iter = keys.iterator();iter.hasNext();){

String keyStr = (String) iter.next();

System.out.println(keyStr+"---"+map.get(keyStr));

}

//遍历Map的第二种方式,先获取所有的键值对,键值对直接获取值

Set<Entry> set2 = map.entrySet();

for(Iterator iter = set2.iterator();iter.hasNext();){

Entry e = (Entry) iter.next();

System.out.println(e.getKey()+"---"+e.getValue());

}

}

1.5 并发修改异常

运行结果:

API中认识一下这个异常:

2种解决办法:

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表