1) Nothing is ever inserted into the queue.

2) Queues usually have push()/pop() methods which insert/remove elements.
The element at the front is usually accessed with a front() method.

3) Instead of pre-allocating an Array, you may wish to use a Vector for
priority, or in this case, a TreeMap may be ideal.  A TreeMap implements
SortedMap, and guarantees that the keys and values of the mapping can be
enumerated in ascending order of keys.  The issue with this is that the
get() method returns an Object, which you have to cast to the correct
type.  You will need to use Integers instead of ints.  If the assignment
disallows use of Java's collections, then disregard this.

4) I don't understand the use of 2 arrays. what is the where[] array for?

5) Your primary problem here is this: scoping.  Take a look at your
constructor for scoping issues.  Is a definition hiding another
definition?

Good luck.

-Chris McKinley
finger lamfada at lugh.net for GPG key


On Sun, 3 Mar 2002, Phil Mendelsohn wrote:

> Hi,
>
> I'm picking up some Java on the fly, and am making what is probably a
> simple oversight type of error.  Could someone tell me why I'm having
> trouble accessing arrays of objects in an object from a class's member
> functions?
>
> Here is the beginning of the class, with the constructors.  It's a
> little messy -- there's been some initialization code added for
> debugging.  The constructor seems to work fine, but though it
> compiles, when I call it in another program (some test code) it just
> hangs as soon as it tries to read or write values in the arrays.
>
> I've been liberal with the 'public' statements; the reason (debugging
> / desperation!) should be clear, and is not an issue for what I'm
> doing.  This is a U homework thing, but it's not a Java class (it's
> Algorithms), so no fear about academic integrity in answering.
>
> Help is appreciated!
>
> Phil Mendelsohn
>
> --
> www.rephil.org
>
> "Trying to do something with your life is like
> sitting down to eat a moose." --Douglas Wood
>
> -----------
>
> public class PriorityQueue{
>
>     public qItem [] Q;
>     public int qSize;
>     public int [] where;
>
>     public class qItem{
> 	public int item;
> 	public int key;
>
> 	public qItem(int item, int key){
> 	    //consructor
> 	    this.item = item;
> 	    this.key = key;
> 	}
>     }
>
>
>     public PriorityQueue(int maxItem){
> 	//constructor of an empty queue: see below
> 	qItem [] Q = new qItem[maxItem];
> 	int [] where = new int[maxItem];
> 	for( int j=0; j < maxItem; j++){
> 	    where[j] = -1;
> 	    Q[j] = new qItem(-2, -3);
> 	    System.out.println(where[j] + " " + Q[j].item +
> 			       " " +  Q[j].key);
> 	}
> 	int qSize = 0;
>     }//end constructor
>
>
>     void insert(int item, int key){
> 	//insert the item with given key as its priority
> 	qSize++;
> 	System.out.println("qSize:  " + qSize);
> 	System.out.println("Item: " + item);
> 	System.out.println("Key: " + key);
> 	for(int i = 0; i < qSize; i++){
> 	    System.out.println(Q[i].item);
> 	    System.out.println(Q[i].key);
> 	}
>     }
> _______________________________________________
> tclug-devel mailing list
> tclug-devel at mn-linux.org
> https://mailman.mn-linux.org/mailman/listinfo/tclug-devel
>