views:

223

answers:

3

Hi,

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

A: 

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.

AaronM
The design itself is flawed as was pointed out. I was just following a template laid out by my professor. The reason the array is processed starting with 1 instead of 0 is it makes the output simpler later. Flawed yes but unavoidable at this point. I do like the idea of using the array.length however. Thanks for the information on the NullPointer references. The initalization loop for the Array is what I needed. I also added in some error checking to make sure that the element being referenced is not null. Thanks for all the help!
David Bobo
+2  A: 

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>();
Etaoin
Works now, it cleared up while adding the loop initializing each element in the array. Maybe IDE bug?
David Bobo
+1  A: 

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.

Stefan Kendall
Noob question, but what do you mean by proper collection type Stefan? Programming isn't my main area of focus (Computer Engineering is my major) but I really would like the better understand proper technique.
David Bobo
Use a list instead of an array to enforce the semantics you're trying to achieve. With your array, array[i] = new ArrayList<String>() would compile. If instead of an array, you used a List<Type>, this wouldn't compile. Ideally, you catch software errors as early as possible, and if you can catch an error like this at compilation, which you can, you should strive to achieve this.
Stefan Kendall
This is exactly what I am looking for..
satyajit