为了方便这里就使用ArrayList来进行演示
ArrayList<String> arr = new ArrayList<>();
arr.add("alix");
arr.add("jack");
arr.add("mary");
迭代器的使用
Iterator<String> iterator = arr.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
查看hasNext()和next()底层
/** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements * 如果迭代器还有更多元素则返回true */ boolean hasNext(); /** * Returns the next element in the iteration. * * @return the next element in the iteration