- class EnhancedForDemo{
- public static void main(String[] args) {
- int[] numbers=new int[10];
- for (int j=0;j<numbers.length;j++ ) {
- numbers[j]=j;
- }
- long time1=System.nanoTime();
- for (int item : numbers) {
- System.out.print(item+" ");
- }
- long time2=System.nanoTime();
- for(int i=0;i<numbers.length;i++){
- System.out.print(numbers[i]+" ");
- }
- long time3=System.nanoTime();
- System.out.println();
- System.out.println(time2-time1);
- System.out.println(time3-time2);
- }
- }
复制代码
结果
明明for语句比增强的for语句效率高那么多? |
|