JAVA basics List

Source: Internet
Author: User

Overview: 1. the ordered (INDEX) Order is not the order, which is generated by comparison with the Set statement). You can repeat the Set. list is a sub-interface of Collection, so List can use all Collection methods. (See my blog article) 3. in addition, List also has its own special method, index-related operations (This article focuses on this part of the Introduction) 4. prevent lying down and being shot 5. listIterator: list-specific iterator (this example is not described in detail, please refer to the api) 6. two basic implementation classes: ArrayList (non-thread security) and Vector (thread security) 1. vector is an old legacy class (poor performance) and is not recommended. 2. the data structure of the Vector (Stack) Stack, which is replaced by the empty list. arrays. arrayList is a fixed-length List, which can only be traversed and cannot be operated 8. listing List 1. it can be used for dual-end queue and stack 2. because it is saved in the form of a linked list, the random access performance is poor, but the iteration, insertion, and deletion performance is better, because you can directly change the pointer address (Data Structure Knowledge) Java code: package com. cxy. collection; import java. util. arrayList; import java. util. arrays; import java. util. using list; import java. util. list;/*** @ author cxy */public class ListTest {public static void main (String [] args) {List l = New ArrayList (); l. add ("Kindergarten"); l. add ("Elementary School"); l. add ("Kindergarten"); l. add ("Middle School"); l. add (""); System. out. println (l); // you can see that there are duplicate data systems. out. println ("the element whose index is 3 is:" + l. get (3); System. out. println ("index first appeared in kindergarten:" + l. indexOf ("Kindergarten"); System. out. println ("the last index of the kindergarten:" + l. lastIndexOf ("Kindergarten"); l. set (3, "Junior High School"); // similar to the modification operation l. add (4, "High School"); // insert System. out. println (l); l. remove ("Kindergarten"); // The first kindergarten System found is deleted. out. println (l ); L. remove (new AA (); // because the equals method of AA returns true, the first element is mistaken for being equal to AA, so the System is shot while lying down. out. println (l); l. remove (new BB (); // The equals method of BB returns false, so no equal elements are found to delete the List. System. out. println (l); l. remove (0); // delete System by serial number. out. println (l); ArrayList l1 = new ArrayList (); l1.ensureCapacity (64); // large data set. This method can be used to allocate space at a time to avoid frequent allocation, improve Performance l1.trimToSize (); // adjust to the used space to avoid Memory waste. Try {List temp = Arrays. asList ("Elementary School", "Middle School", ""); temp. remove (0);} catch (Exception e) {System. out. println ("temp is a fixed-length list that can only be traversed and cannot be operated");} your list ll = new your list (); ll. add ("aaa"); ll. add ("bbb"); ll. add ("ccc"); ll. add ("ddd"); System. out. println ("Original:" + ll); ll. addLast ("zzz"); // queue -- enter ll. removeFirst (); // queue-out System. out. println ("queue operation:" + ll); ll. addFirst ("kkk"); // Stack -- stack System. out. println ("kkk inbound Stack:" + ll); ll. removeFirst (); // stack-out stack System. out. println ("kkk out Stack:" + ll); ll. removeLast (); // represents the dual-end queue System. out. println ("=================="); ll = new shortlist (); ll. add ("aaa"); ll. add ("bbb"); ll. add ("ccc"); ll. add ("ddd"); System. out. println ("reinitialize:" + ll); ll. push ("kkk"); // Stack -- into the stack System. out. println ("kkk inbound Stack:" + ll); ll. pop (); // Stack -- out stack System. out. println ("kkk out Stack:" + ll); ll. offer (""); // queue -- enter ll. offerFirst (""); // returns true for addFirst ll. offerLast (""); // returns true addLast ll. poll (); // obtain and delete the first ll. pollFirst (); // obtain and delete the first ll. pollLast (); // obtain and delete the last ll. peekFirst (); // get the first ll. peekLast (); // get not to delete the last ll. removeFirstOccurrence ("aaa"); // Delete the first aaa ll. removeLastOccurrence ("aaa"); // Delete the last aaa} class AA {@ Override public boolean equals (Object obj) {www.2cto.com return true ;}} class BB {@ Override public boolean equals (Object obj) {return false ;}}

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.