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: queue question

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question queue question

    I need some general help with queues. I am able to conceptualize with no problem what my queue should be doing, but implementationis rough for me.

    I am trying to make a program with queues representing a 4way intersection. Vehicles approach the intersecion and are queued up until the light turns green. I understand how to add objects to my queue and how/when to remove them correctly. I don't understand how to create the vehicle objects without defining each one individually.

    Since the vehicles arrive with a random probability, which means I wont know how many there will be, is there a way to create objects as necessary rather than declaring them in the beginning, then push them into my queue?

    This is probably really basic, but I missed this if it is.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: queue question

    I'm not really sure what you're asking. You might want to user a Timer and Math.random() or the Random class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: queue question

    Hi Herah,

    Something like this perhaps?

    Queue<Car> myQ = new Queue<Car>();
     
    // after some random period of time
     
    myQ.add(new Car());

    Regards,

    Chris

  4. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Herah (November 15th, 2011)

Similar Threads

  1. Implementing a priority queue using a max heap
    By jsinclair1482 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:56 AM
  2. Question about blocking queue
    By Kerr in forum Threads
    Replies: 2
    Last Post: April 8th, 2011, 04:59 AM
  3. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 10:02 PM
  4. Implementing a queue or stack using a heap
    By tla280 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 1st, 2010, 12:29 AM
  5. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM

Tags for this Thread