Created attachment 30 [details] Actual result Overview: Pressing reset button do not reset the table after submitted input Steps to Reproduce: 1) Input value(s) to any textfield in the recommendation panel (I inputted "Acer" in the Brand textfield) 2)Press the Submit button (after pressing the submit button the table should sort out laptops of brand Acer) 3)Press the Reset button Actual Results: The table remained unchanged Expected Results: The table should display the original data array list without any sorting Build Date & Hardware: Build 2019-11-15 on Mac OS 10.14.5 & Windows 10
Debug steps: 1) reproduced the bug as mentioned 2) print out the array list to see what happened Observations: The original array list has been modified from the submit action, then added to the table. For each sorting/searching action, the original array list (imported data from txt file) should not be modified, a copy of the original array list should be created instead for any data processing so that the reset button can reset the table to the default state. Suggested Solution: In every function we need to process the data and return a new one to be displayed, we create a new array list to hold the array list passed in.
(In reply to Matthew from comment #1) > Debug steps: > 1) reproduced the bug as mentioned > > 2) print out the array list to see what happened > > Observations: > The original array list has been modified from the submit action, then added > to the table. > > For each sorting/searching action, the original array list (imported data > from txt file) should not be modified, a copy of the original array list > should be created instead for any data processing so that the reset button > can reset the table to the default state. > > Suggested Solution: > In every function we need to process the data and return a new one to be > displayed, we create a new array list to hold the array list passed in. Bug update: The suggested solution does not work, even a new array list [laptopList] is created to hold the copy of the original array list [allLaptops], when we made changes to [laptopList], it modified [allLaptops] as well. Simplified function with bug: public recommend (allLaptops, ...){ ArrayList<Laptop> laptopList = allLaptops; . . . return laptopList; }
(In reply to Matthew from comment #2) > (In reply to Matthew from comment #1) > > Debug steps: > > 1) reproduced the bug as mentioned > > > > 2) print out the array list to see what happened > > > > Observations: > > The original array list has been modified from the submit action, then added > > to the table. > > > > For each sorting/searching action, the original array list (imported data > > from txt file) should not be modified, a copy of the original array list > > should be created instead for any data processing so that the reset button > > can reset the table to the default state. > > > > Suggested Solution: > > In every function we need to process the data and return a new one to be > > displayed, we create a new array list to hold the array list passed in. > > Bug update: > The suggested solution does not work, even a new array list [laptopList] is > created to hold the copy of the original array list [allLaptops], when we > made changes to [laptopList], it modified [allLaptops] as well. > > Simplified function with bug: > public recommend (allLaptops, ...){ > ArrayList<Laptop> laptopList = allLaptops; > . > . > . > return laptopList; > } Suggested Solution: Seems the declaration of laptopList cannot direct point to allLaptops, the expression of [ArrayList<Laptop> laptopList = allLaptops;] will create a new array list pointing to the address of each element of allLaptops. Therefore, when we make changes to laptopList, it actually modified the value of that element in both laptopList and allLaptops. We can declare the new array list in this way: ArrayList<Laptop> laptopList = new ArrayList<Laptop>(allLaptops); It should first create a clean and new array list without referencing the original array list, then copy every elements from allLaptops to laptopList.