ArrayList Java 的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:55:54  来源:igfitidea点击:

Array of ArrayList Java

javaarraysarraylist

提问by David Bobo

I am creating an PriorityQueue with multiple queues. I am using an Array to store the multiple ArrayLists that make up my different PriorityQueues. Here is what I have for my constructor so far:

我正在创建一个具有多个队列的 PriorityQueue。我正在使用一个数组来存储构成我不同 PriorityQueue 的多个 ArrayList。到目前为止,这是我的构造函数的内容:

ArrayList<ProcessRecord> pq;
ArrayList[] arrayQ;

  MultiList(){       
   arrayQ = new ArrayList[9];
   pq = new ArrayList<ProcessRecord>();
 }

The problem comes when I am trying to get the size of the entire array, that is the sum of the sizes of each ArrayListin the array.

当我试图获取整个数组的大小时,问题就出现了,即数组中每个大小的总和ArrayList

public int getSize() {

    int size = 0;

    for (int i = 1; i <= 9; i++) {
        size = size + this.arrayQ[i].size();
    }
    return size;
}

is not seeming to work. Am I declaring the Array of ArrayListcorrectly? I keep getting an error saying that this.arrayQ[i].size()is not a method. (the .size() being the problem)

似乎不起作用。我ArrayList是否正确地声明了数组?我不断收到错误消息,说这this.arrayQ[i].size()不是一种方法。( .size() 是问题所在)

Thanks for any help!

谢谢你的帮助!

David

大卫

回答by Etaoin

Some problems:

一些问题:

First of all, arrays in Java are zero-indexed, so your loop should read:

首先,Java 中的数组是零索引的,所以你的循环应该是:

for (int i = 0; i < 9; i++)

Or, better, replace the magic number 9 by arrayQ.lengthto make your life easier if the length changes.

或者,更好的是,arrayQ.length如果长度发生变化,将神奇数字 9 替换为使您的生活更轻松。

Second, you aren't filling your array with ArrayLists -- new ArrayList[9]creates an array of nine references of type ArrayList, but all those references are null. After creating the array in your constructor, you'll need to instantiate the ArrayLists themselves, by doing something like this:

其次,你没有用 ArrayLists 填充你的数组——new ArrayList[9]创建了一个包含九个 type 引用的数组ArrayList,但所有这些引用都是空的。在构造函数中创建数组后,您需要通过执行以下操作来实例化 ArrayLists 本身:

for (int i = 0; i < arrayQ.length; i++)
    arrayQ[i] = new ArrayList<ProcessRecord>();

回答by Stefan Kendall

Also of note: I'm not sure what you're doing, but why are you mixing arraysand ArrayLists? This is almost certainly a poor design decision, and you would be better off using an ArrayList<ArrayList<Type>>.

另外值得注意的是:我不确定你在做什么,但你为什么要混合arraysArrayLists?这几乎肯定是一个糟糕的设计决定,最好使用ArrayList<ArrayList<Type>>.

I see that you're working around your inherent design failure by using an array of ArrayList, and not ArrayList<ProcessRecord>, but this isn't typesafe, and you should just be using a proper collection type.

我看到您正在通过使用ArrayList, 而不是的数组来解决您固有的设计失败ArrayList<ProcessRecord>,但这不是类型安全的,您应该只使用正确的集合类型。

回答by AaronM

You need to put in some null checking. Your for loop assumes that there's something in that it can operate on, but you're calling the .size() method on null elements, which doesn't work, it will throw a NullPointerException.

您需要进行一些空检查。您的 for 循环假定它可以操作某些东西,但是您正在对 null 元素调用 .size() 方法,这不起作用,它将抛出 NullPointerException。

You will also have some problems with your loop. Array indexing begins at 0, not 1. Your loop begins at 1, and since you're using it to access the array, you will be starting with the second element, and trying to access 1 element beyond the scope of the array, which will throw an ArrayIndexOutofBoundsException.

你的循环也会有一些问题。数组索引从 0 开始,而不是 1。您的循环从 1 开始,并且由于您使用它来访问数组,因此您将从第二个元素开始,并尝试访问超出数组范围的 1 个元素,即将抛出一个 ArrayIndexOutofBoundsException。