package seday11;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;

/**
* @author xingsir
* JDK5之后推出了一个特性:增强for循环也称为新循环,for each.
* 新循环不是用来取代传统for循环的操作,而仅用来遍历集合或数组使用。
*/
public class NewForDemo {

public static void main(String[] args) {
int[] num= {1,2,3,4,5};
for(int i=0;i<num.length;i++ ) {
int k=num[i];
System.out.println(k);
}
for(int k:num) {
System.out.println(k);


Collection c = new ArrayList();
c.add("1");
c.add("2");
c.add("3");
c.add("4");
c.add("5");
System.out.println(c);
/*
* 编译器会将新循环遍历集合改为迭代器遍历,所以在遍历的过程中不能通过集合的方法增删元素
*/
for(Object o:c) {
String i=(String)o;
System.out.println(i);
}

}
}
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-01-09
  • 2021-12-06
  • 2022-01-22
  • 2021-12-07
  • 2022-12-23
猜你喜欢
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-04-14
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案