语法
示例
import java.util.collection;
public class foreach
{
private collection c = null;
private string[] belle = new string[4];
public foreach()
{
belle[0] = "西施";
belle[1] = "王昭君";
belle[2] = "貂禅";
belle[3] = "杨贵妃";
c = arrays.aslist(belle);
}
public void testcollection()
{
for (string b : c)
{
system.out.println("曾经的风化绝代:" + b);
}
}
public void testarray()
{
for (string b : belle)
{
system.out.println("曾经的青史留名:" + b);
}
}
public static void main(string[] args)
{
foreach each = new foreach();
each.testcollection();
each.testarray();
}
}
在以前的代码中,我们可以通过iterator执行remove操作。
但是,在现在的foreach版中,我们无法删除集合包含的对象。你也不能替换对象。
同时,你也不能并行的foreach多个集合。所以,在我们编写代码时,还得看情况而使用它。
如对本文有疑问, 点击进行留言回复!!
java编程入门 常见问题0002:如何将你的ecpilse工程在同学的eclipse上恢复
SpringBoot入门教程07——整合mybatis-plus(三)
SpringBoot集成Flowable-modeler模块并去除权限认证(一)
《剑指offer》—— 66. 机器人的运动范围(Java)
网友评论