简体   繁体   English

改组LinkedList时发生ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException when shuffling a LinkedList

Okay, so I have an assignment and it goes like this: 好的,我有一个作业,它像这样:

Assume you have as input a singly-linked list containing N items (an instance of a LinkedList class in Java). 假设您有一个包含N个项目的单链接列表(Java中LinkedList类的实例)作为输入。 You should rearrange the items in the LinkedList uniformly at random. 您应该随机地均匀地重新排列LinkedList中的项目。 Your algorithm should consume a logarithmic (or constant) amount of extra memory and run in time proportional to NlogN in the worst case. 您的算法应消耗对数(或常数)的额外内存,并在最坏的情况下按与NlogN成比例的时间运行。

For the Algorithm I was going for mergesort which when it merges does so in a random way. 对于算法,我打算使用mergesort,它在合并时会以随机方式进行。 But I have gotten stuck with my program as it throws an ArrayIndexOutOfBoundsException but it's not in my code but in LinkedList.java 但是我被程序卡住了,因为它抛出ArrayIndexOutOfBoundsException但是它不在我的代码中,而在LinkedList.java中

I'm not very familiar with LinkedList so I have no idea what to do, I'm not even sure I'm doing this in the right way. 我对LinkedList不太熟悉,所以我不知道该怎么做,我甚至不确定自己是否以正确的方式进行操作。

The code: 编码:

import java.util.LinkedList;

public class Q02 {

public static void main(String[] args) {

    LinkedList<Integer> list = new LinkedList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    list.add(5);
    list.add(6);
    list.add(7);
    list.add(8);
    list.add(9);
    list.add(0);
    System.out.println("Shuffled list: "+shuffle(list));
}

public static LinkedList shuffle(LinkedList list){
    int node = 10; //just a random node
    if(list.size()<=1){
        return list;
    }
    LinkedList<Integer>list1 = new LinkedList<>();
    LinkedList<Integer>list2 = new LinkedList<>();

    while(!list.isEmpty()){
        list1.add((Integer) list.removeFirst());
        if(!list.isEmpty()){
            list2.add((Integer) list.removeFirst());
        }
    }
    shuffle(list1);
    shuffle(list2);

    if(list2.size() < list1.size()){
        int i = (int)(Math.random() * list2.size());
        list2.set(i, node);
    }

    while(!list1.isEmpty()&&!list2.isEmpty()){
        int rand = (int)(Math.round(Math.random()));
        if(rand == 1){
            list.add(list1.removeFirst());
        }
        else if(rand == 0){
            list.add(list2.removeFirst());
        }
    }
    //If any of list1 or list2 are still empty add everything to list
    if(!list1.isEmpty()){
        list.add(list2.clone());
    }
    if(!list2.isEmpty()){
        list.add(list1.clone());
    }
    list.remove(node);
    return list;
}

}

Here is the error: 这是错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 2
at java.util.LinkedList.checkElementIndex(LinkedList.java:553)
at java.util.LinkedList.remove(LinkedList.java:523)
at kth.id2010.lab.lab03.Q02.Q02.shuffle(Q02.java:62)
at kth.id2010.lab.lab03.Q02.Q02.shuffle(Q02.java:39)
at kth.id2010.lab.lab03.Q02.Q02.shuffle(Q02.java:39)
at kth.id2010.lab.lab03.Q02.Q02.shuffle(Q02.java:39)
at kth.id2010.lab.lab03.Q02.Q02.main(Q02.java:22)

If you are getting an ArrayIndexOutOfBoundsException , that means that somewhere you are trying to access an item in the LinkedList using an index that is invalid. 如果获取ArrayIndexOutOfBoundsException ,则意味着您尝试使用无效索引访问LinkedList某个项目的位置。 For example, if your LinkedList has 10 items, then listOfSize10.set(10, node) would raise an ArrayIndexOutOfBoundsException . 例如,如果您的LinkedList10项目,则listOfSize10.set(10, node)将引发ArrayIndexOutOfBoundsException

Most likely the error you are seeing stems from these lines: 您最可能看到的错误是由以下几行引起的:

int i = (int)(Math.random() * list2.size());
list2.set(i, node);

Can you guarantee that i is always greater than or equal to 0 and less than list2.size() ? 您可以保证i始终大于或等于0且小于list2.size()吗?

Update After seeing your stack trace, it looks like the error comes from these lines: 更新在看到您的堆栈跟踪之后,看起来错误来自以下行:

int node = 10; //just a random node
list.remove(node);

Here you are clearly trying to remove the item at index 10 (or, the eleventh item) when there are only 10 items in the list. 当列表中只有10项目时,您显然在这里尝试删除索引为10的项目(或第11个项目)。 So your array index is out of bounds. 因此,您的数组索引超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM