Cause: After each remove element of the list, the subsequent elements move forward,

Source: Internet
Author: User
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;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.