专注于高品质PHP技术等信息服务于一体 [STIEMAP] [RSS]

百度提供的广告:
java
当前位置:首页 > 技术文档 > java >  > 
java当中最有效率的遍历List

package com.qingruxu.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qingruxu.opendb.open;
import com.qingruxu.vo.EcsGoods;

public class Main {
    public static void main(String[] args) {
        ApplicationContext content=new ClassPathXmlApplicationContext("applicationContext.xml");
        open op=(open) content.getBean("open");
        //op.selectGoods();
        List data =op.getGoods(35, 1);
        long start=System.nanoTime();
        for(int i=0;i<data.size();i++)
        {
            EcsGoods ecs=(EcsGoods) data.get(i);
        }
        long stop=System.nanoTime();
       
        System.out.println("for用时:"+(stop-start));
      
        Iterator it= data.iterator();
        start=System.nanoTime();
        while(it.hasNext()){
            EcsGoods ecs=(EcsGoods) it.next();
        }
        stop=System.nanoTime();
        System.out.println("Iterator用时:"+(stop-start));
        start=System.nanoTime();
        for (Object object : data) {
            EcsGoods ecs=(EcsGoods) object;
        }
        stop=System.nanoTime();
        System.out.println("foreach用时:"+(stop-start));
    }

}