Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Priority Queue using comparable

1. ## Priority Queue using comparable

Hey,

Im trying to make a Prioity Queue that uses the queue interface and comparable.. The objects will be placed in depending on their priority.. i'm having a bit of trouble with it, probaly cause i'm quite new to Comparables..
I have the following but am having trouble even understanding what it is my code is doing!!

```	public int compareTo(Object o)
{
// 1:   films duration is greater than 'o'
if(this.duration > ((Film)o).duration)
return 1;
// 0:   films duration equals 'o'
else if(this.duration == ((Film)o).duration)
return 0;
// -1:   films durations is less than 'o'
return -1;
}```

```public void enqueue (Comparable o)
{

if(isEmpty())
{
int temp =rear;
rear= (rear + 1) % queue.length;
if (front ==rear)
{
rear = temp;
throw new FullQueueException ();
}
queue[rear] = o;
}

else
{

for(int i = 0; i <= queue.length; i++)
{
//duration is greater so everything must be moved right
if(o.compareTo(queue[i]) == 1 )
{
for(int x = 0; x <= queue.length; x++)
{
int temp =rear;
rear= (rear + 1) % queue.length;
if (front ==rear)
{
rear = temp;
throw new FullQueueException ();
}
queue[rear] = queue[rear+1];
}
queue[front] = o;
}

if(o.compareTo(queue[i]) == 0 )
{

}```

Any help appreciated

2. ## Re: Priority Queue using comparable

I presume you are writing this yourself as an exercise or homework? Java provides the PriorityQueue class.

What exactly is the problem you are having, as its not obvious (at least to me) from the posted code?

3. ## Re: Priority Queue using comparable

I take it that the variable duration is a number? You can just subtract the two numbers. Also, Comparable interfaces must implement what they are comparable to (generally you don't want this to be a wild-card or the base Object class)

```public class Film implements Comparable<Film>
{
// ... other code

public int compareTo(Film o)
{
return this.duration - o.duration;
}
}```

`public void enqueue (Comparable<Film> o)`

4. ## Re: Priority Queue using comparable

I think we have to make it generic and able to work with any kind of object?? Ye its a class exercise.. im just wondering if my enqueue method is on the right track.. "Joining the queue (enqueue)will involve finding the correct position for the new item, based on its priority, i.e. all the items before it in the queue should have >= priority; all items after it should have a lower (<) priority."

5. ## Re: Priority Queue using comparable

I kind of doubt it, it makes no sense to try and compare say a BufferedImage object to your Film object.

6. ## Re: Priority Queue using comparable

Ok, ill give it try.. Am i along the right track with what i have though??

7. ## Re: Priority Queue using comparable

I'm having trouble trying to move all elements right in this queue. For example, I need to place something in between others because it's priority says it's in there, so how would i make space for it in my circular array queue?? thanks again, hope i'm making sense!