Package hb.list;
Import Hb.util.Person;
Import Java.util.Iterator;
Import java.util.LinkedList;
Import java.util.List;
public class Removelistobject {public static void main (string[] args) {LinkedList list = new LinkedList ();
for (int i = 0; i < 6; i++) {person p = new person ();
P.setname ("name" + i);
P.setpassword ("password" +i);
List.add (P);
}//linkedlist has a clone method, but the interface list is not a list List2 = (list) list.clone ();
Printlist (list); The length of the Traverse must be list.size (), because after removing the object will cause the list length to change, if the use of constants will result in boundary anomalies//For example: length is 100, delete 50, and get the list Object 51, will explode the boundary exception,
Therefore, use List.size () for (int i=0;i<list.size (), i++) {Person obj = (person) list.get (i);
if (Obj.getname (). Equals ("Name3")) {list.remove (i);
The For loop is the first to determine if it is true based on the median value, and then executes the subsequent i++ i--;
}} printlist (list);
Generally we use Iterator it = List2.iterator ();
while (It.hasnext ()) {Person tempobj = (person) it.next ();
if (Tempobj.getname (). Equals ("Name2")) {//Removes the current object It.remove ();
}
}Printlist (LIST2);
} public static string printlist (List list) {string result = "";
for (int i=0, i< list.size (); i++) {result = result + (person) list.get (i)). GetName () + "----";
} System.out.println (Result);
return result;
}
}