首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

List 遍历步骤及比较

2012-12-24 
List 遍历方法及比较package cn.jellen.system.actionimport java.util.ArrayListimport java.util.Iter

List 遍历方法及比较

package cn.jellen.system.action;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
?* List 遍历方法及比较
?* @author KH
?*??? 有时候复制、粘贴出现的错误:Syntax error, insert ";" to complete ReturnStatement 删除多余的空格,重新格式化一下就OK喽!
?*/
public class Test {

??? public static void main(String args[]){

??? ? List<Long> lists = new ArrayList<Long>();
??? ?? for(Long i=0L;i<888888;i++){
??? ??? lists.add(i);
??? ?? }
??? ?? Long oneOk = oneMethod(lists);
??? ?? Long twoOk = twoMethod(lists);
??? ?? Long threeOk = threeMethod(lists);
??? ?? Long fourOk = fourMethod(lists);
??? ?? System.out.println("One:" + oneOk);
??? ?? System.out.println("Two:" + twoOk);
??? ?? System.out.println("Three:" + threeOk);
??? ?? System.out.println("four:" + fourOk);
??? ??
??? ?? /* 参考结果
??? ??? One:13625
??? ??? Two:10797
??? ??? Three:10594
??? ??? four:10750
??? ?? */
??? }

??? public static Long oneMethod(List<Long> lists) {
??? ??? Long timeStart = System.currentTimeMillis(); //返回以毫秒为单位的当前时间
??? ??? for (int i = 0; i < lists.size(); i++) {
??? ??? ??? System.out.println(lists.get(i));
??? ??? }
??? ??? Long timeStop = System.currentTimeMillis();
??? ??? return timeStop - timeStart;
??? }

??? public static Long twoMethod(List<Long> lists) {
??? ??? Long timeStart = System.currentTimeMillis();
??? ??? for (Long string : lists) {
??? ??? ??? System.out.println(string);
??? ??? }
??? ??? Long timeStop = System.currentTimeMillis();
??? ??? return timeStop - timeStart;
??? }

??? public static Long threeMethod(List<Long> lists) {
??? ??? Long timeStart = System.currentTimeMillis();
??? ??? Iterator<Long> it = lists.iterator();
??? ??? while (it.hasNext())
??? ??? {
??? ??? ??? System.out.println(it.next());
??? ??? }
??? ??? Long timeStop = System.currentTimeMillis();
??? ??? return timeStop - timeStart;
??? }

??? public static Long fourMethod(List<Long> lists){
??? ?? Long timeStart = System.currentTimeMillis();
??? ?? for(Iterator<Long> i = lists.iterator(); i.hasNext();){
??? ??? ?? System.out.println(i.next());
??? ?? }
??? ?? Long timeStop = System.currentTimeMillis();
??? ?? return timeStop - timeStart;
??? }
}

热点排行