collection的框架以及collection的使用和迭代器的使用

collection接口的使用

collection的框架以及collection的使用和迭代器的使用collection接口
collection的框架以及collection的使用和迭代器的使用因为collection集合不可以用索引值来获取元素,所以遍历collection只能用迭代器iterator实现对collection的遍历。
1,通过调用iterator()方法返回一个迭代器,迭代器有一个游标,新获得的迭代器游标指向第一个元素前面。
2,迭代器有一个hasNext() 用来判断游标后面是否还有其它元素,返回值是Boolean类型
3,迭代器有next()方法 ,返回游标后面的内容,然后把游标向后移动。
//遍历collection 使用迭代器
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
System.out.println("next = " + next);
}