Java中foreach一般用于遍历数组和容器,与for语句最大的区别是:for可以通过设置不同的step值(step=1...)来遍历数组或容器中的元素,而foreach必须遍历数组或容器中的每个元素。
其中,
1.对于数组Array,for(Element e : Array)会根据Array.length的大小决定遍历次数。
2.对于容器Collection,for(Element e : Collection)会根据Collection.size()的大小决定遍历次数。
其中,
1.对于数组Array,for(Element e : Array)会根据Array.length的大小决定遍历次数。
2.对于容器Collection,for(Element e : Collection)会根据Collection.size()的大小决定遍历次数。