增强for循环、迭代器(遍历列表)

也称foreach循环。

作用:

遍历数组或集合中的元素。

用法:

LinkedList link=new LinkedList();

link.add("stu1");

link.add("stu3);

for(object obj:link)

   System.out.println(obj);

迭代器:

Iterator it=link.Iterator();

while(it.hasNext())

{

Object obj=it.next();

System.out.println(obj);

}

用迭代器移除元素

一、

while(it.hasNext())

{

if("stu3".equals(obj))

{

link.remove(obj);//但这种迭代器并不知道,添加个Break语句。

break;

}

}

二、

if("stu3".equals(obj)){

it.remove();

}

猜你喜欢

转载自blog.csdn.net/qiuyushuofeng/article/details/81387950