Java Experience--collection, list __java

Source: Internet
Author: User

1.:

Collection: (in util package)

Set: (preface, Element not repeatable)

List: (preface, repeatable; index by corner):

ArrayList: The underlying data type is an array, adding and deleting is slow, find (access) Fast

LinkedList: The underlying data type is linked list, additions and deletions fast, find (access) slow.

Vector: The underlying data type is an array; the elder level, thread safe, but additions and deletions and lookups are unpleasant, subclasses: Stack (Advanced out).

2. Collection: variable length, can be stored in different data types, only storage objects.

array: The length is immutable, you can save the same data type, store objects and basic data types.

3. Note: arrays, StringBuffer, and even the various collection types described in the following article are a "container" that can hold data and objects.

Java uses a collection class to hold different kinds of data, which is built on an unknown basis, that is, Java uses a finite set of classes to accommodate an infinite variety of data objects.

Category: Java collection classes can be grouped into three categories: sets, lists, and mappings

4. Listing (list)

The list lists the order of elements, that is, the list can precisely control where each element is inserted, and the user can access the elements in the list using the index (the location of the element in the list). Unlike set collections, the list allows you to include duplicate elements.

5. Italic identifies the interface, and a dashed arrow indicates an "implementation" relationship

                               

need to note:

JAVASDK does not provide classes that inherit directly from collection, and the Java SDK provides classes that are inherited from collection "Sub-interfaces."

The collection interface has multiple sub-interfaces, of which the most important two java.util.Set and Java.util.List describe set sets and list lists respectively.

Methods in collection:

Publicboolean Add (E E): Adds an element to the collection and joins the successful return ture. The method splits in its sub-interface, for example, when the set interface adds a duplicate element, it is rejected and returns false, and the list interface accepts the repeating element and returns to Ture.

publicboolean Remove (Object o): Removes the specified element from the collection

publicint size (): Returns the number of elements, that is, traversing the collection.

Public booleancontains (Object obj): Determines whether the specified element is included

Publicboolean IsEmpty (): Determining if the collection is empty

publicobject[] ToArray (): Returns an array containing all the elements in the current collection.

Public Void Clear (): Delete all elements

6. Regardless of the actual type of collection, it supports a method-public Iteratoriterator (), the method (iterator as an interface, contains three methods) returns an enumerator (iterator) object, Using this method, you can traverse each element in the collection. Usage:

method One:

Iteratorit = C.iterator (); C is a collection object

The While (It.hasnext ()) {//hasnext () method determines whether there is a next element.

Object obj = It.next ();//next () Gets the next element, returns an object, and at the beginning, the pointer is in front of the first element.

}

Method Two:

For (Iteratorit = C.iterator (); It.hasnext ();) {

Object obj = It.next ();

}

method Two is better, one is code concise; The second is that the new IT object is not in the loop, while the loop remains, but the for loop can be reclaimed by the garbage collection mechanism.

7. The Java.util.List interface describes the list structure, allowing the programmer to accurately control the insertion position of the list element and increase the ability to access elements and search elements based on the element's index. On the basis of inheriting the parent interface collection, the list interface adds a corresponding method:

voidadd (int index, E Element): Inserts an object after the index number

boolean Add (Ee): Inserts an object e into the list last

eremove (int index): Delete the specified index number element

booleanremove (Object o): Deletes the first specified content element of a list

eget (int index): The element that gets the specified index number, when the generic is not used, the return value type is object, so in this case the return object is coerced to its original type, depending on the actual.

intsize (): Half-Interface

intindexof (Object obj): Finds an OBJECT element, returns its index value

list<e>sublist (int fromindex, int toindex): Lot Chain (a method with a beginning ending, all containing the beginning does not contain the end)

voidclear ()

the implementation class (subclass) of the Java.util.List interface has multiple, representing the different forms of the list.

ArrayList, Vector, Stack, LinkedList

8.Vctor class

The list interface is solid, for the "vector" in the data type structure.

@@@ 构造 Method:

Vctor (): Constructs an empty vector, sets its initial value to 10, and the standard capacity increment is 0;

Vctor (intinitialcapacity): Constructs an empty vector using the specified initial value and the capacity increment for 0;

vctor (Intinitialcaption,int capacipyincrement): Constructs an empty vector using known initial values and capacity increments. Most commonly used in actual projects.

Vctor (<e>c): This constructed method embodies the new features of Java-generics, which can be used only if you know exactly what type of object it holds before using Vctor.

@@@ 其他 methods:

voidadd (E obj): Adds an element to the collection that embodies the generic attribute

insertelementat (eobj,int index): Adding an element at the specified index

setelementat (eobj,int index): Replaces the element at the specified index, similar to the previous method

Publicboolean removeelement (Object obj): Deletes the specified element from the collection to delete the first Obj object to indicate whether the specified object is found and deleted in the Vctor.

void Removeelementat (Intindex): Deletes the specified index element

void Removeallelement (): Deletes all elements, deletes all objects in Vctor, and resets the size of the Vctor object to 0

publicint size (): Returns the number of elements, that is, traversing the collection.

eelementat (int index): accesses the elements in the vctor in turn by using this method to return the element with the index number I in the object and the generic or object

The position (corner) inserted in the method parameter in Vctor is behind the object.

9. Advanced Stack class

a stack is a subclass of a vector class, characterized by a container of the "LIFO" (last Infirst out) type, that is, an object that was pushed into the stack by a "push", which is the first "pop".

  

 

@@@ 构造 Method

Stack (): Used to create objects that support the "LIFO" access method
Example: Stack st=new stack ();
Stack <String> st = Newstack ();

@@@ 其他 Methods

Epeek () returns the top element of the stack, but does not eject the top element of the stack

the E pop () pops up the top element of the stack and returns the object.

Epush (E Item) presses the item object onto the top of the stack and returns the item object.

Booleanempty () determines whether the stack is empty, returns True if the stack is empty, and returns false.

Note: Because the stack inherits the vector class, the following statement is syntactically not problematic. But it destroys the "LIFO" feature of the stack, so it is not recommended.

st.addelement ("Badusage1");

st.addelement ("Badusage2");

st.addelement ("Badusage3");

For (Inti=0;i<st.size (); i++) {

System.out.println (St.elementat (i));

}

10.LinkedList class: Chained storage-side mode

Logically adjacent two elements are physically not adjacent, connected by "chain"

@@@ 构造 Method:

LinkedList ()

LinkedList (COLLECTIONC): Collection is the base class of a set of linear table classes in Java This construction method can load the elements within collection into the LinkedList object.

LinkedList (<e>c)

@@@ 其他 methods:

To add an element:

void AddFirst (eobj) and void AddLast (E obj): Adds an element to the table header and footer, based on the method of add in the list interface.

Get element:

e GetFirst (E obj) and objectgetlast (E obj): An extension of the method of get in the list interface: fetching elements at the table header and footer.

To Delete an element:

e Removefirst (E obj) and objectremovelast (E obj): Adds an element to the table header and footer, based on the method of remove in the list interface.

11. Array:

(1) You can quickly navigate to any element in an array based on the starting address of the array and the index number of the element in it .

(2) Inserts, deletes the element and so on operation efficiency bottom

Linked list:

(1) The cost of accessing elements in a linked list is higher than that of an array.

(2) can easily complete the insertion and deletion of elements of the work

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.