LinkedList时间遍历的迭代器

时间:2016-03-18 20:27:10

标签: java linked-list iterator traversal

我编写了一个程序,它在linkedlist中存储了一些整数,并且还使用迭代器和get(index)测试了它遍历列表的时间,我对我的程序100%肯定但是当我运行时程序它给了我这个错误:

  

线程中的异常" main" java.lang.IndexOutOfBoundsException:索引:   20000,尺寸:20000 at   java.util.LinkedList.checkElementIndex(LinkedList.java:555)at   java.util.LinkedList.get(LinkedList.java:476)at   Homework2.MyLinkedList.main(MyLinkedList.java:72)

您可以在下面找到我的代码。我需要帮助

import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;

public class MyLinkedList {
  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    // System.out.print("Enter the size of your list: ");
    // int size = in.nextInt();

    int n1 = 20000;
    int n2 = 200000;
    int n3 = 2000000;

    LinkedList < Integer > list1 = new LinkedList();

    for (int i = 1; i <= n1; i++) {
      list1.add(i);
    }

    long t1Start = System.currentTimeMillis();
    ListIterator < Integer > iterator1 = list1.listIterator();
    while (iterator1.hasNext()) {
      iterator1.next();
    }
    long t1Stop = System.currentTimeMillis();

    long t1Final = t1Stop - t1Start;

    //////////////////////////////////////////

    LinkedList < Integer > list2 = new LinkedList();

    for (int i = 1; i <= n2; i++) {
      list2.add(i);
    }

    long t2Start = System.currentTimeMillis();
    ListIterator < Integer > iterator2 = list1.listIterator();
    while (iterator1.hasNext()) {
      iterator1.next();
    }
    long t2Stop = System.currentTimeMillis();

    long t2Final = t2Stop - t2Start;

    //////////////////////////////////////////

    LinkedList < Integer > list3 = new LinkedList();

    for (int i = 1; i <= n2; i++) {
      list2.add(i);
    }

    long t3Start = System.currentTimeMillis();
    ListIterator < Integer > iterator3 = list1.listIterator();
    while (iterator1.hasNext()) {
      iterator1.next();
    }
    long t3Stop = System.currentTimeMillis();

    long t3Final = t3Stop - t3Start;

    //////////////////////////////////////////

    long get1Start = System.currentTimeMillis();
    for (int i = 1; i <= n1; i++) {
      list1.get(i);
    }
    long get1Stop = System.currentTimeMillis();
    long get1Final = get1Stop - get1Start;

    //////////////////////////////////////////

    long get2Start = System.currentTimeMillis();
    for (int i = 1; i <= n2; i++) {
      list2.get(i);
    }
    long get2Stop = System.currentTimeMillis();
    long get2Final = get2Stop - get2Start;

    //////////////////////////////////////////

    long get3Start = System.currentTimeMillis();
    for (int i = 1; i <= n3; i++) {
      list3.get(i);
    }
    long get3Stop = System.currentTimeMillis();
    long get3Final = get3Stop - get3Start;

    //////////////////////////////////////////

    System.out.println("n\t\titerator,seconds\t\tget(index),seconds");
    System.out.println(n1 + "\t\t" + t1Final + "\t\t" + get1Final);
    System.out.println(n2 + "\t\t" + t2Final + "\t\t" + get2Final);
    System.out.println(n3 + "\t\t" + t3Final + "\t\t" + get3Final);

  }
}

1 个答案:

答案 0 :(得分:5)

问题在于

for (int i = 1; i <= n1; i++) {
    list1.get(i);
}

索引从0到大小 - 1.当i等于n1时,您得到IndexOutOfBoundsException,因为列表的最大索引是n1 - 1。将其更改为

for (int i = 0; i < n1; i++) {
    list1.get(i);
}

同样适用于list2list3

此外,正如@Paul Boddington所提到的,你永远不会向list3添加任何内容,但是你会将值两次插入list2