Solved: PriorityQueues and Array Lists

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

klam

Thread Starter
Joined
Apr 21, 2006
Messages
147
Hi there,
I'm trying to implement a priority queue using arrays. Now, what's the difference between an arraylist and an array?

I saw this implementation in Java:
Code:
public void insert (Comparable item) {
        if (index == array.length) {
            resize ();
        }
        array[index] = item;
        index++;
    }
and I also saw something similar in C++, now.. independently from the Programming language I'll be using.. it doesn't look like it's inserting the item by priority.. it looks like it's inserting it on the last index of the array and increasing the position to the next index.. to me it looks like a common queue.. where am i lost?
 
Joined
Jul 29, 2004
Messages
6,650
In Java, an array is a kind of table which must contain object of the same type.

In your example (Link), each item of the object array is a Comparable object :

Code:
[SIZE=2][B]
public class PriorityQueue {
    private Comparable[] array;
    private int index;
}
[/B][/SIZE]

ArrayList, more apparented to the object Vector is a kind of array where items may be all different objects.

Example :

Code:
[SIZE=2][B]
ArrayList arlist = new ArrayList();

String firstObject = "Whatever your want";
Comparable secondObject = null;

arlist.add(firstOject);        // the ArrayList contains 2 different objects
arlist.add(secondObject);      // 

// If you want to transform ArrayList into a 'genuine' array

Object[] myArray = arlist.toArray();    // myArray is a array of Object

// If you want to get the value of the first item in myArray
String myFirstObject = (String) myArray[0];

// If you want to get the value of the second item in my array
Comparable mySecondObject = (Comparable) myArray[1];
[/B][/SIZE]
 

klam

Thread Starter
Joined
Apr 21, 2006
Messages
147
So the difference is that one allows you to input objects of different types.. ok, interesting... thanks Chicon.

And for the rest, I just found a solution to it... so this's been resolved (y)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top