问题
I am trying to create a table of tasks and manipulate its contents. I am tackling with a bug I don't understand, and be very thankful for help. I applogoze for the length of the question, but trying to bring as much details as possible.
The class tasksRepository stores the records, which are constructed in the class taskDef, as array list. The setTask () and getTask methods contains the add() and get() methods of the ArrayList class.
The taskDef object's parameters are created using local set methods. The following is an example to such method: If the delivered parameter differs from the stored value it will be stored value and the event will be added to the log record. In any case a success flag is retuned.
public static Boolean setProjectNumber(String projectNumberI)
{ // All the
if (projectNumberI != projectNumber)
{
projectNumber = projectNumberI;
logAccumulator = logAccumulator + "; project number (" + projectNumberI + ")";
}
return true;
}
private static void setLog ()
{ // When all the parameters are set, the log parameter is updated with time tag
String t = formatterFull.format(GregorianCalendar.getInstance().getTime());
log = log + "\n" + t + ": " + logAccumulator;
}
The class tasksTableProcessing contains the tasks manipulation. The following contains the constructor. It was started in a simpler form, but during the investigation I changed it. The class taskStatus is ENUM that returns strings. The method getLog () simply returns the log field
public tasksTableProcessing ()
{ // constructor. Creates the tableRepository object with test records
tasksRepository.clearDb ();
taskDef task = null;
task = new taskDef ("P1000", "O1", taskStatus.started (), "1", "false", "false");
tasksRepository.addTask (task);
System.out.println("\nLog 0: \n" + tasksRepository.getTask(0).getLog());
task = null;
task = new taskDef ("P1000", "O1", taskStatus.started(), "2", "false", "true");
tasksRepository.addTask (task);
System.out.println("\nLog 1: \n" + tasksRepository.getTask(1).getLog());
task = null;
task = new taskDef ("P2000", "O2", "closed", "63", "true", "false");
tasksRepository.addTask (task);
System.out.println("\nLog 2: \n" + tasksRepository.getTask(2).getLog());
task = null;
task = new taskDef ("P2000", "O2", "closed", "64", "true", "true");
tasksRepository.addTask (task);
System.out.println("\nLog 3: \n" + tasksRepository.getTask(3).getLog());
}
The following is the log. It shows that when a new record is introduced, all the old records are updated.
Log 0:
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started)
Log 1:
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true)
Log 2:
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true); project number (P2000); task (Task 63); task ID (63); owner (O2); status (closed); critical path (true); delivery flag (false)
Log 3:
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true); project number (P2000); task (Task 63); task ID (63); owner (O2); status (closed); critical path (true); delivery flag (false)
09/07/2012 10:21: ; project number (P1000); task (Task 1); task ID (1); owner (O1); status (started); task (Task 2); task ID (2); delivery flag (true); project number (P2000); task (Task 63); task ID (63); owner (O2); status (closed); critical path (true); delivery flag (false); task (Task 64); task ID (64); delivery flag (true)
回答1:
Given the method signatures, you store the members of your TaskDefs as static fields, rather than instance fields. A static field is a field of the class TaskDef itself, and not a field of a specific TaskDef instance.
Read the Java tutorial about instance and class members.
来源:https://stackoverflow.com/questions/11392179/erroneous-update-of-arraylist-records-when-a-new-record-is-added