Home > Blockchain >  Remove rows from LinkedList
Remove rows from LinkedList

Time:12-07

In my anylogic model, I want to add orderID, processing time and due date to a list on enter of a queue block. This works fine. Now I would like to remove this information from the list at the moment that the order leaves the queue, so on exit, but I can't get it to work.

I have the following:

class ExportInfo {
    private int id;
    private int P;
    private double d;

    public ExportInfo(int id, int P, double d) { 
           this.id=id;
           this.P=P;
           this.d=d;
    }
}

LinkedList<ExportInfo> list = new LinkedList();

At my queue block on enter:

new ExportInfo(agent.atrID, agent.atrEstimatedPackingTime, dateToTime(agent.atrDueDate));

list.add(new ExportInfo(agent.atrID, agent.atrEstimatedPackingTime, dateToTime(agent.atrDueDate)));

On exit:

new ExportInfo(agent.atrID, agent.atrEstimatedPackingTime, dateToTime(agent.atrDueDate));
list.remove(new ExportInfo(agent.atrID, agent.atrEstimatedPackingTime, dateToTime(agent.atrDueDate)));

At some point, I write the results to an Excel file. I first checked the results if I only add the info to the list. In the Excel file I see all the info of the orders that came in (let's say there where 6).

Then I also tried with remove, which would mean that at the moment the info is written to Excel, it should only contain info from orders that have not left the queue (since in that case their info should have been removed from the list). I can see that there are 4 orders in the queue at the moment i write info to my Excel file, however still the information of all the 6 orders that entered the queue is there, so the info of the 2 orders that have left the queue has not been removed.

Does anyone know how I can solve this?

CodePudding user response:

remove will not find the object, because you constructed a new object which by consequence is not in the list yet.

You should either:

  1. Retain a reference to the object that you created at the time you added it to the list, and use that reference when calling remove.

  2. (Less efficient:) Iterate the list and look for an object that has the properties you are looking for, and then remove it.

CodePudding user response:

You are constantly adding and removing new instances of ExportInfo, this does not make sense.

Add entries once in the queue block on enter. Also, store the ExportInfo inside the actual agent as a variable myExportInfo so it remembers it until the queue exit.

In the Queue exit, you want to change the exit time in your ExportInfo object, but not create a new one. So use a setter for the queue exit time, like agent.myExportInfo.setExitTime(...)

This will give you a full list of all agents that passed through the queue

  • Related