LinkedList链表的遍历(迭代器遍历和get循环遍历)

package oj.test;

import java.util.*;
public class Demo4 {

 /**
  * @LinkedList链表的遍历
  */
 public static void main(String[] args) {
  LinkedList link = new LinkedList();
  for(int k=1;k<=34567;k++){
   link.add(new Student("i am",k));
  }
  Iterator it = link.iterator();
  long time1 = System.currentTimeMillis();
  while(it.hasNext()){
   Student s = it.next();
  }
  long time2 = System.currentTimeMillis();
  sop("使用迭代器遍历用时:"+(time2-time1)+"毫秒");
  
  time1 = System.currentTimeMillis();
  for(int i=0;i    Student s2 = link.get(i);
  }
  time2 = System.currentTimeMillis();
  sop("使用get遍历用时:"+(time2-time1)+"毫秒");
  

 }
 
 public static void sop(Object obj){
  System.out.println(obj);
 }

}

class Student{
 String name;
 int number;

 Student(String name,int number){
  this.name = name;
  this.number = number;
  
 }
 
}

你可能感兴趣的:(Java基本知识)