On my understanding of the collection framework-List

Source: Internet
Author: User
Tags addall


Collection Frame (collection)


Description of the collection:

When you have more data, you encapsulate it in an object, and when the object is more, it is encapsulated in the collection.

In fact, a collection is a way to store objects.


Arrays differ from Collections:

Array: The length is fixed, you can store the base data type, and you can store the object.

Collection: The length is variable, and only the object is stored.


Set Frame system:


The following is an introduction to the 2 gang List under the Collection interface, Set


List (ordered collection)------------------------------------

List of 2 younger brothers: ArrayList, LinkedList list features: elements are ordered, can store duplicate elements, because the list has an index, the bottom is the array structure.

--------------------------------------------------------------------------------------------------------------- ----------------------

ArrayList Example:

		/* Create */ArrayList Al = new ArrayList (); Creates an empty parameter ArrayList object, initialized with a capacity of ArrayList a2 = new ArrayList (20); Creates a ArrayList object and specifies that the initialization capacity is ArrayList a3 = new ArrayList (A2);


		Creates a ArrayList object and passes in the ArrayList object, initializing the data. 	  /* Add */Al.add ("abc"); add element al.add (0, "qwe");    add element under specified position (cannot exceed size ()-1) al.addall (A2); Add an object under collection Al.addall (0, A2); From the specified location, add the object under collection/* Delete */Al.remove ("abc");     Delete content based on element al.remove (0);       Delete content based on index al.clear ();


		Removes all the elements from this list. /* Modify */Al.retainall (A2); 2 elements are compared, returning the same element Al.removeall (A2); 2 elements compared, delete the same element Al.set (0, "QQQQ");       Specify the index to modify the corresponding element of the index to QQQQ/*/Al.isempty (); The Judgment list is empty al.contains ("abc");		Determines whether the list contains the specified element ABC/* GET */al.size (); 		Returns the length of the list Al.clone ();
		Returns the current instance object. 		Al.get (0);      Based on the index, find the element Al.indexof ("abc");  Depending on the element, find the index, and find the return index bit, otherwise-1 al.lastindexof ("abc"); Depending on the element, look for the index, find the return index bit, otherwise-1, start from right to left List sub = al.sublist (0, 2);
		            Specifies the start end-1 range, which returns a list object,                 This needs to be noted, the return is a reference address, not the newly established//need to note that the sub call the Remove method to delete an element, the elements of the Al object will also be deleted. String[] s = (string[]) Al.toarray (new string[0]);

 Converts an Array object.


LinkedList instances:

<pre name= "code" class= "java" >	
	        linkedlist L2 = new LinkedList (ll);//Create LinkedList object and pass in LinkedList object, initialize data.

		/* It has a ArrayList method, here no longer allocations *

		/* Below is a unique method of LinkedList *

		/Ll.offerfirst ("Q");  Each time the element
		Ll.offerfirst ("W") is inserted from the first position;  Each time the element
		Ll.offerfirst ("E") is inserted from the first position;  Each time the element
		Ll.offerfirst ("R") is inserted from the first position;  Each time the element

		Ll.offerfirst ("a") is inserted from the first position;  Each time the element
		ll.offerlast ("1") is inserted from the first position;   Each time the element is inserted from the last position

		Ll.peekfirst ();      Each time it gets the first element
		ll.peeklast ();       Each time it gets the last element

		Ll.pollfirst ();	     Each time you get the first element, delete the element
		ll.polllast (); 	     Each time you get the last element, delete the element

		ll.element ();  	     Gets the first element without deleting the element
		Ll.offer ("1111");    Inserts the element at the end.
		Ll.peek ();  	     Gets the first element without deleting the element
		Ll.poll ();  	     To delete an element after getting the first element


Common ways to traverse a collection:

		Arraylist<integer> al = new arraylist<integer> (); 

		for (int i = 0; i<5; i++)
		{
			al.add (i);
		}

		Collection of common traversal methods, through the iterator traversal element
		Iterator it = Al.iterator ();
		while (It.hasnext ())
		{
			System.out.println (It.next ());
		}

		/* Note that it.next () two times is not allowed in iterators, otherwise the nosuchelementexception exception is thrown *  //////////* Iterator supports the delete operation call It.remove () method */

		//Traverse for
		(Integer a:al)
		{
			System.out.println (a) through the advanced for loop;
		}



List Collection-specific iterators

		Listiterator li = Al.listiterator ();

		while (Li.hasnext ())    //forward traversal
		{
			System.out.print (Li.next () + "");
		} 

		System.out.println ();

		while (li.hasprevious ())//Reverse traversal
		{
			System.out.print (li.previous () + "");
		}
		System.out.println ();

		/* In the process of traversal, you can make a search
			/*/* LI.ADD ();        Add
			Li.set (5);       Modify
			li.remove ();     Delete

			li.next ();       Positive get element
			li.previous ();   Reverse Get Element

			Li.hasnext ()     //Judge Next has no element
			li.hasprevious ()//Judge the previous have no element
		*/






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.