Results 1 to 3 of 3
  1. #1
    Cheddz is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Nested List class for Simulation Queue

    Hello, all. This is my first post to the forums, and I hope someone out there can help me! For a client, I am developing a (fairly) sophisticated discrete-event simulation written entirely in Java. For anyone familiar with simulations, before an entity can be serviced, it must enter a queue. In version 0.1 of the simulation there was only one queue, a Queue1 class, with a nested List class inside of it, which just initiated an empty list. The Queue1 class added or removed entities from the queue. The List class looked as follows:
    Java Code:
    class List {
    	// Global variables for List
    	public Commodity work;
    	public List next;
    	
    	public List() {
    		work = null;
    		next = null;
    	}
    }
    Now, I need another queue. We can call it Queue2, for kicks. If I try to add another class List {} inside Queue2, it says List has already been defined.

    So, I tried creating a generic Queue class with the list nested, and had my Queue classes, Queue1 and Queue2, extend Queue. However, both queues are still feeding off of the same List. How do I create new lists in each individual queue class so that I can maintain different lists simultaneously? If you would like the code for one of the Queue classes, I'd be glad to post that, as well. Thanks!

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Why aren't you just using a LinkedList?

    Java Code:
    LinkedList<Commodity> queue1 = new LinkedList<Commodity>();
    LinkedList<Commodity> queue2 = new LinkedList<Commodity>();
    Edit:

    If threads are involved: ConcurrentLinkedQueue
    Last edited by mrmatt1111; 02-18-2010 at 10:55 PM.
    My Hobby Project: LegacyClone

  3. #3
    Cheddz is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Well that's definitely an implementation I can look into, but the List is currently called if a double length == 0, thus creating a new list. For some reason, the second process is reading off the same length value as the first process, and thus never evaluates to true, and passes right by it. However, length is a private variable in both classes. The enqueue method looks as follows:

    Java Code:
    	public void enqueue(Commodity commodityToAdd) {
    		if(commodityToAdd == null) 
    			return;
    		
    		List ptr = head;
    		
    		if(isEmpty()) {
    			head = new List();
    			ptr = head;
    		}
    		else {
    			while(ptr.next != null) 
    				ptr = ptr.next;
    			
    			ptr.next = new List();
    			ptr = ptr.next;
    		}
    		
    		ptr.next = null;
    		ptr.work = commodityToAdd;
    		length++;
    	}
    There is also a dequeue method which should decrement the length variable.

Similar Threads

  1. beehive simulation
    By BlueF4re in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 09:31 AM
  2. nested collapsing list
    By roohja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-05-2009, 08:15 AM
  3. Queue List
    By nahid in forum Advanced Java
    Replies: 4
    Last Post: 10-08-2008, 09:50 AM
  4. Mars Simulation Project 2.84
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 06-26-2008, 07:18 PM
  5. Pulley Simulation..HELP
    By dazza-s in forum Java 2D
    Replies: 2
    Last Post: 06-18-2008, 11:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •