java - What's an efficient algorithm to make one list equal to another one? -


Assuming I have a List A that should look exactly like a list. All the objects of B are but A does not have to be added to A and A has all the objects, but there are no b, they should be removed from A.

I need a reason for this because I have an ArrayList of players, I save a file every time when I update the attributes of a player, then I save the changes to the file, Which calls a method that looks at the player's ArrayList and saves it, it works because the names of the Arrivites are references.

However, whenever I search for a player in the list, I read the file and update the list where it is stored. It changes all contexts with all new objects after doing so, if I change the user brought in earlier and try to save it, I have changed the new instance of the player in exchange for it.

To create a list equal to any other solution, you have to come up with a good algorithm? Or is there a better way to update the entire list while keeping references in using it?

Update: Update solution, O (NOLMM) runs in time, loops through each element in the destination, search for it in the source if it has been found, has been removed from the source if not, then destination Removed, then add the remaining element to the destination in the destination. The list must definitely be sorted, but the list received from the file will already be sorted because I sort.

  Import java.util.Collections; Import Java.util.Comparator; Import java.util.list; Public class copy {public static zero copy (list, list, source) {copy (destination, source); / / The remaining elements in the Src list would be those that were originally // but not sure. And so they are dest.addAll (src); Should be added; } Public static void copy (list, list, verify source v) {copy (dest, src); // The remaining elements in the source list would be those that were not originally in // in the // source and therefore would have to add addAll (dest, src, v); } Public Static Zero Copy (List Gallery, List Source, Comparative C) {Copy (DSTC, CRC, C); / / The remaining elements in the Src list would be those that were originally // but not sure. And so they are dest.addAll (src); Should be added; } Public static zeros copy (list of inventory, source of the list, collateral c, verify v) {copy (dest, src, c); // The remaining elements in the source list would be those that were not originally in // in the // source and therefore would have to add addAll (dest, src, v); } Private Static Zero Copy (List Dat, List Solution) {// To go through the DIST list, find out whether each element is on the new list // Travel backwards from Travel because we have its elements (int i = Dest). Size () - 1; I & gt; = 0; I--) {int src_i = Archive. Binary search (src, dest.get (i)); If (src_i & gt; = 0) // If the element is found in the src index, delete it from the src list src.remove (src_i); Else // If the element is not found in the src list, remove it from dest list destremove (i); }} Private Static Zero Copy (List Gallery, List Source, Comparative C) {// To go through the destination list, if every element is in new list // Go backwards from the trip because the elements can be removed (Int i = dest .isize () - 1; i> = 0; i--) {int src_i = Archive. Binary search (src, dest.get (i), c); If (src_i & gt; = 0) // If the element is found in the src index, delete it from the src list src.remove (src_i); Else // If the element is not found in the src list, remove it from dest list destremove (i); }} Private static zero addAll (list, list source, verify v) {// src index (object o: src) before verifying each object if (v.verify (o)) dest.add (o) ; }}  

What would be the simplest list to copy B? In this, copying the built-in array requires a lot less time and effort compared to just one complex algorithm (not to mention easy to mention).


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -