1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PriorityQueues and Array Lists

Discussion in 'Software Development' started by klam, Jan 25, 2007.

Thread Status:
Not open for further replies.
  1. klam

    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?
     
  2. Chicon

    Chicon

    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]
     
  3. klam

    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)
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/538423

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice