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.

Results 1 to 3 of 3

Thread: Help with queues

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with queues

    just got a question on queues. i need to Write a client program that creates three queues of integers: an ArrayQueue, a ListQueue and a DequeQueue. For each of these time how long it takes to add and remove 100 000 random integers, repeating this 100 times in order to get measurable results.

    Could somebody please steer me in the right direction on how to complete this? im fairly new with java and the documentation on arrayQueues etc confuses me. Many thanks in advanced


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with queues

    All objects are constructed using the object constructer (well, almost all. certain objects hide their constructors and have you call a factory constructor). To do so, you use the "new" keyword and the class name.

    Unfortunately, those classes are not part of the standard Java API, so I'm just guessing at how they are to be used. I am going under the assumption that all of them implement the Queue Interface (this is a link the Java's SE API).

    The Queue-type classes also require generic parameters. This is to tell Java what type of object this queue is allowed to hold. These are passed through the <>.

    It is important to note that generics need to be classes, so you can't simply pass in "int", which is a primitive data type.

    new ArrayQueue<int>(); // Error! int is a primitive data type!

    Here's a simple snippet to show how to construct an ArrayQueue of Integers the correct way.

    // create a new ArrayQueue
    ArrayQueue<Integer> myArrayQueue = new ArrayQueue<Integer>();

    There are two simple ways to generate random numbers (well, actually creating your own isn't too hard but I'm going to ignore this option for now), but the easier way is to use a Random object. Simply create a Random object, and then use the nextInt() method to generate a Random number between 0 and the number you pass as a parameter.

    // generate a random number from 0 - 10. Includes 0, but not 10
    Random myGenerator = new Random();
    myGenerator.nextInt(10);

    To add items to the queue, you can use the offer() method. This tries to add an element to the queue, and returns true/false depending on if it succeeded or failed respectively. To remove items, use the remove() method. This will automatically remove the item most recently added.

    // add a few Integers and remove them
    ArrayQueue<Integer> myQueue1 = new ArrayQueue();
    myQueue1.offer(5);
    myQueue1.offer(4);
    System.out.println("The first item you added was " + myQueue1.remove());
    System.out.println("The second item you added was " + myQueue1.remove());

    Obviously, for 100000 items you will want to use a for loop. To see how to use for-loops, see The for Statement(The Java Tutorials).

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with queues

    thank you sooo much for the reply...i am going to attempt this tonight with the ArrayQueue and the Deque and post them up on here. i actually understand now thanks

Similar Threads

  1. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 01:28 PM