//集合的遍历方式1:增强for循环 //collection没有下标 无法使用普通for循环 //使用增强for循环来遍历
for(Object o :coll1) { String s = (String) o; System.out.println(o); } //从非泛型集合中拿出元素必须强行转换类型 //尽量保存同一数据类型//iterator方法返回迭代器对象. // 哪个集合调用的迭代器方法产生的对象就迭代这个集合 //next方法无法读取到元素时,返回异常. // hasNext能限制next方法不抛出异常
Iterator iter = coll.iterator(); while(iter.hasNext()){ Object o1 = iter.next(); String s1 = (String) o1;//迭代器迭代集合时, //不允许集合自行添加删除.但是可以使用迭代器添加删除 }