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:
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 ArrayList in the array.
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 ArrayList correctly? I keep getting an error saying that this.arrayQ[i].size() is not a method. (the .size() being the problem)
Thanks for any help!
David
-
Although I don't think it's the cause of your problem, that for loop is going to skip the first element in the array entirely, and throw an ArrayIndexOutOfBoundsException when it gets to the end. Java arrays are zero-indexed, so the valid indexes for a 9-element array are 0 through 8.Syntactic– Syntactic2010年04月07日 01:15:40 +00:00Commented Apr 7, 2010 at 1:15
-
Why don't you use just one array or a heap for implementing the PriorityQueue?helpermethod– helpermethod2010年04月07日 01:33:51 +00:00Commented Apr 7, 2010 at 1:33
-
I have used both in past assignments, this one was supposed to be multiple PriorityQueues and outputting them in a largest priority round-robin style for each queue.David Bobo– David Bobo2010年04月07日 01:53:44 +00:00Commented Apr 7, 2010 at 1:53
3 Answers 3
Some problems:
First of all, arrays in Java are zero-indexed, so your loop should read:
for (int i = 0; i < 9; i++)
Or, better, replace the magic number 9 by arrayQ.length to make your life easier if the length changes.
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:
for (int i = 0; i < arrayQ.length; i++)
arrayQ[i] = new ArrayList<ProcessRecord>();
2 Comments
ArrayList instances contained in arrayQ, consider: ArrayList<ProcessRecord>[] arrayQ = (ArrayList<ProcessRecord>[]) Array.newInstance(ArrayList.class, 9);. In this way, accessing the ProcessRecord from the ArrayList element of arrayQ will not require additional type casting.Also of note: I'm not sure what you're doing, but why are you mixing arrays and ArrayLists? This is almost certainly a poor design decision, and you would be better off using an 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.
2 Comments
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.
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.