Yesterday, a project team member needed to delete all elements not equal to the specified value in the arraylist. However, after a long time, she found that they were not completely deleted. As I have done similar functions before, I told her that the arraylist has reduced the length of the deleted element and the index of the element will change, but the subscript of the iteration has not changed accordingly.
Summarize some deletion methods:
1 /** 2 * Delete the element whose value is "C" in the arraylist. 3 */ 4 Public Static Void Main (string [] ARGs ){ 5 6 List <string> List = New Arraylist <string> (); 7 8 // "C" is not sequential stored in arraylist 9 /* 10 List. Add ("C "); 11 List. Add (""); 12 List. Add ("C "); 13 List. Add ("B "); 14 List. Add ("C "); 15 List. Add ("D "); 16 List. Add ("C "); 17 */ 18 19 // "C" has continuous storage in arraylist 20 List. Add ("" ); 21 List. Add ("C" ); 22 List. Add ("C" ); 23 List. Add ("B" ); 24 List. Add ("C" ); 25 List. Add ("C" ); 26 List. Add ("D" ); 27 List. Add ("C" ); 28 29 30 // Deletes an element whose value is "C" in the arraylist. 31 32 // You may not be able to delete all 33 // Removelistelement1 (list ); 34 35 // Can be deleted correctly 36 // Removelistelement2 (list ); 37 38 // Can be deleted correctly 39 // Removelistelement3 (list ); 40 } 41 42 43 /** 44 * Delete the element whose value is "C" in the list. 45 * 46 * This method: 47 * 48 * When an element whose value is "C" is not stored consecutively in the arraylist, you can delete all the elements whose value is "C ". 49 * 50 * When an element with a value of "C" is stored continuously in the arraylist, all the elements with a value of "C" are not deleted. 51 * If an element is deleted, the length of the arraylist becomes smaller and the index changes, but the subscript of the iteration does not decrease. 52 */ 53 Public Static Void Removelistelement1 (list <string> List ){ 54 For ( Int I = 0; I <list. Size (); I ++ ){ 55 If ("C". Equals (list. Get (I ))){ 56 List. Remove (I ); 57 } 58 } 59 60 } 61 62 /** 63 * Delete the element whose value is "C" in the arraylist. 64 * 65 * This method: 66 * 67 * You can delete all elements with a value of "C" from the arraylist, regardless of whether the elements with a value of "C" are consecutive. 68 */ 69 Public Static Void Removelistelement2 (list <string> List ){ 70 For ( Int I = 0; I <list. Size (); I ++ ){ 71 If ("C" . Equals (list. Get (I ))){ 72 List. Remove (I ); 73 -- I; // The element is deleted, and the subscript of the iteration also changes. 74 } 75 } 76 } 77 78 /** 79 * Delete the element whose value is "C" in the arraylist. 80 * 81 * This method: 82 * 83 * You can delete all elements with a value of "C" from the list, regardless of whether the elements with a value of "C" are consecutive. 84 * 85 * Make sure that no other threads are modified at the same time. 86 */ 87 Public Static Void Removelistelement3 (list <string> List ){ 88 Iterator <string> iterator = List. iterator (); 89 While (Iterator. hasnext ()){ 90 String STR =Iterator. Next (); 91 If ("C" . Equals (STR )){ 92 Iterator. Remove (); 93 } 94 95 } 96 }