The Add method of list in Java does not check whether the list contains items that are joined
The Add method of list in Java does not check whether the list contains items that are joined
The Add method of list in Java does not check whether the list contains items that are joined
Important thing to say three times
The list adds a reference and does not check to see if it has been added to the list
This also means: if you manage a bunch of recycled objects, the list should not be chained to the list and should be a stack, otherwise, as the chain pushes the objects that are actually available to be squeezed into the garbage heap and then recycled, the listing looks small but actually only a few actual reference objects, Any action on the object, such as setting a visible property or setting a value, can have an immeasurable effect on the use of the list.
And each time you add a list, if you have a unique requirement for the elements in the list (usually there), you need to check if it already exists in the list.
The test code is attached:
1Fucker B =Newfucker ();2Fucker A =Newfucker ();3 a.oops ();4 b.oops ();5 6list<fucker> lbs =NewArraylist<>();7 8 for(intI= 0;i<10;i++) {9 Lbs.add (a);Ten Lbs.add (b); One } A - A.fuck (); - for(inti=0;i<10;i++) { theLbs.get (0). Fuck (); -Lbs.remove (Lbs.get (0)); -Lbs.get (0). Fuck (); -Lbs.remove (Lbs.get (0)); +}
Fucker:
1 Public classFucker {2 Private intValue=0;3 4 Public voidfuck () {5System.out.println ("Oooo!");6 System.out.println (value);7Value = 0;8 }9 Ten Public voidOops () { OneValue = 1; ASystem.out.println ("changed!"); - } - the}
In fact, generally this kind of object should be not management, but brother does not trust Java garbage collection mechanism, and I firmly believe that he setvisible (View.gone) that the View does not exist in the development of the method will be a destructive barrier, so the compromise chose a bit ...
Java Pit-----Repeating the same object in the list