#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Help me please. Java Queue


    hello guys Im stuck here, how can you get the first element inside the queue? I put elements in the queue and I should get the first element in that queue and use it. so on.
    here is the code: Please Disregard (queue.size()==1) It is where I do not know what code I should put there. please help me.thankyou

    if(queue.size()<=31){
    queue.offer( incomingCustomer ); // enqueue the customer
    }

    if(queue.size()==1){
    if(tellerTime1==0){
    if (!queue.isEmpty() ) { // ... so get the first customer in the queue
    int timeInQueue1;

    servedCustomer = queue.poll(); // dequeue the customer, i.e. poll() method
    // does the dequeue operation

    tellerTime1 = servedCustomer.transLength;
    timeInQueue1 = time-servedCustomer.arrivalTime;

    System.out.printf("\tCustomer #%d leaves queue. Waited %d minutes in the queue.\n",
    servedCustomer.id, timeInQueue1 );

    System.out.printf("\tTeller1 is busy with Customer #%d. " +
    "%s will be completed in %d minute%s.\n",
    servedCustomer.id, servedCustomer.transaction,
    tellerTime1, tellerTime1>1?"s":"" );

    }
    }
    }
    if(queue.size()==2){
    if(tellerTime2==0){
    if(!queue.isEmpty()){
    int timeInQueue2;
    servedCustomer = queue.poll();
    tellerTime2 = servedCustomer.transLength;
    timeInQueue2 = time-servedCustomer.arrivalTime;

    System.out.printf("\tCustomer #%d leaves queue. Waited %d minutes in the queue.\n",
    servedCustomer.id, timeInQueue2 );
    System.out.printf("\tTeller2 is busy with Customer #%d. " +
    "%s will be completed in %d minute%s.\n",
    servedCustomer.id, servedCustomer.transaction,
    tellerTime2, tellerTime2>1?"s":"" );

    }
    }
    }
    if(queue.size()==3){
    if(tellerTime3==0){
    if(!queue.isEmpty()){
    int timeInQueue3;
    servedCustomer = queue.poll();
    tellerTime3 = servedCustomer.transLength;
    timeInQueue3 = time-servedCustomer.arrivalTime;

    System.out.printf("\tCustomer #%d leaves queue. Waited %d minutes in the queue.\n",
    servedCustomer.id, timeInQueue3 );
    System.out.printf("\tTeller3 is busy with Customer #%d. " +
    "%s will be completed in %d minute%s.\n",
    servedCustomer.id, servedCustomer.transaction,
    tellerTime3, tellerTime3>1?"s":"" );
    }
    }
    }
    thanks for the help
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    What class is the Queue object you are using?

    how can you get the first element inside the queue
    What method are you trying to use? There aren't many choices.

    BTW Please use code tags around your code to preserve formatting.
  4. #3
  5. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    Originally Posted by redfoxriders
    how can you get the first element inside the queue? I put elements in the queue and I should get the first element in that queue and use it.
    Originally Posted by redfoxriders
    What class is the Queue object you are using?
    Refer to api documentation of your queue class if it doesnot provide methods for getting first element you can do following:


    java.util Interface Queue implements java.util Interface Collection<E>
    refer methods iterator and toArray of Collection

    Comments on this post

    • jzd disagrees : Queue, does in fact provide a method to get the first element. There is no reason to convert it to an array or use an iterator.
    no one can become perfect by merely ceasing the act
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,923
    Rep Power
    9646
    If it's a Queue then there's Queue.element() and .peek().

    srsly guise

IMN logo majestic logo threadwatch logo seochat tools logo