gpt4 book ai didi

java - 链表时间遍历中的迭代器

转载 作者:行者123 更新时间:2023-12-01 16:53:23 24 4
gpt4 key购买 nike

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

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20000, Size: 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);

}
}

最佳答案

问题出在

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

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

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

list2list3 也是如此。

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

关于java - 链表时间遍历中的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36093652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com