Six collection classes

Source: Internet
Author: User

Arraylist hashtable sortedlist colletionbase stack queue namevaluecollection

1 arraylist class

Represents a one-dimensional array that dynamically increases the size as needed. This can be compared with the system. array class with a fixed size.

Arraylist is of a weak type-they can contain any managed elements, and do not require that all elements are of the same type.

The arraylist has an initial capacity. When an element is added, the size and capacity of the arraylist are automatically increased. We can use the trimtosize () method to delete elements or display changes to the capacity attribute to reduce capacity.

Constructor

For example, public arraylist ()

Publi arraylist (icollection C)

Public arratlist (INT initialcapacity) arraylist () creates an arraylist object. The default initial capacity is

16. If the number of elements reaches this value, the capacity will be doubled. Public static method

Public static arraylist adapter (ilist List)

Public static arraylist fixedsize (arraylist List)

Public static arraylist fixedsize (ilist List)

Public static arraylist readonlt (arraylist List)

Public static arraylist readonlt (ilist list) Example

Class Program

{

Static void main (string [] ARGs) {arraylist Al = new arraylist (); Al. add (100); // Add foreach (INT number in New int [6] {9, 3, 7, 2, 4, 8}) {Al. add (number); // Add method 1} int [] number2 = new int [2] {11,12}; Al. addrange (number2); // Add method 2. remove (3); // remove Al with a value of 3. removeat (3); // remove 3rd arraylist Al = new arraylist (Al. getrange (1, 3); // The New arraylist takes only a portion of the old arraylist as the console. writeline ("Traversal method 1:"); foreach (int I in Al) // do not forcibly convert {console. writeline (I); // Traversal method 1} console. writeline ("Traversal method 2:"); For (INT I = 0; I! = Al2.count; I ++) // The array is length {int number = (INT) al [I]; // The console must be forcibly converted. writeline (number); // Traversal method 2} console. readkey ();}}

2 The hashtable class encapsulates a set of key/value pairs, which are organized according to the hash code of the key. The hash code of an object can be obtained through the implementation of the ihashcodeprovider interface. These keys must be able to access the gethashcode () and equal () methods. They can define these methods themselves or inherit them from one of their parent classes. Each element in hashtable is a dictionaryentry structure, regardless of the type of the key and value. Hashtable will provide better performance than sortedlist, because hashtable elements do not need to be sorted. However, the hashtable value cannot be accessed through subscript because its elements are unordered. Constructor

Public hashtable () Public hashtable (idictionary d) Public hashtable (intcapacity)

3 The sortedlist class encapsulates a set of key/value pairs. The content in the set is sorted by key. Elements can be accessed through keys or subscripts. Sortedlist uses two arrays to store the list elements: one for storing keys and the other for storing values.

Syntax: public class sortedlist: idictionary, icollection, ienumerable

The constructor public sortedlist () Public sortedlist (icomparer comparer) Static Method public static sortedlist synchronized (sortedlist list) synchronized () puts a synchronous (thread-safe) the package is placed outside the specified sortedlist and the encapsulated version is returned.

Public instance method Public Virtual void add (objec key, obje value) Public Virtual void clear () Public irtual object clone () add () add a record with the specified key and value to the sortedlist call. If the key is an empty reference, if the element with the same key already exists in sortedlist, if sortedlist is read-only or has a fixed size, an exception will be thrown. Clear () deletes all records from the sortedlist call and sets the Count attribute to 0. If sortedlist is read-only or has a fixed size, a notsupportedexception exception will be thrown. Clone () returns a shortest copy that calls sorteflist. A shallow copy will contain references to the elements of the initial sortedlist, rather than a copy containing the sortedlist element.

4 colletionbase class

This is a class that can be used as a base class of a strongly typed set. A class derived from colletionbase can access a protected arraylist field, which is used to store the actual members of the set. The colletionbase class defines many protected methods that can be overwritten by a derived class to provide a program that runs before adding elements to a set, deleting elements from the set, or accessing or modifying elements. Syntax public abstract class collectionbase: ilist, icollection, ienumerable public instance method public void clear () Public void removeata (INT index)

The protected instance method colletionbase class provides many protected methods that are called when some events occur. These methods are paired. The first method is called before the operation is executed, and the second method is called before the operation is completed. Protected virtual void onclear () protected virtual void onclearcomplete ()

The onclear () method is called when the clear () method is called. The onclear () method is executed before any collectionbase element is cleared. If you need to perform some operations before the set is cleared, the default implementation should be overwritten by a derived class. Onclearcomplete () is similar to the onclear () method. The only difference is that it is called after the collectionbase element is cleared.

Static void main (string [] ARGs) {sortedlist SL = new sortedlist (); SL ["C"] = 41; SL ["A"] = 42; SL ["D"] = 11; SL ["B"] = 13; foreach (dictionaryentry element in SL) {string S = (string) element. key; int I = (INT) element. value; console. writeline ("{0}, {1}", S, I);} console. readkey ();} 5 Stack class stacks, first in and out. Push method into the stack, pop method out of the stack. Stack is a data structure. It stores data based on the principle of "first-in-first-out". The first data is pushed to the bottom of the stack, and the last data is placed at the top of the stack, data is popped up from the top of the stack when data needs to be read (the last data is read by the first one ). Stack is a special linear table that can only be inserted and deleted at one end. Pile up items in a bucket, first pile in under pressure, then one piece to pile up. Only one of the above items can be taken when you take the data. The heap and fetch operations are carried out on the top, and the bottom is usually not moving. Stack is a data structure similar to bucket accumulation items. One End for deletion and insertion is called the stack top, and the other is called the stack bottom. Insert is generally called push and delete is called pop ). Stack is also known as a post-import, first-out table (LIFO table ).

Class program {stack Sk = new stack (); stack sk2 = new stack (); foreach (int I in new int [4] {1, 2, 3, 4 }) {SK. push (I); // fill in sk2.push (I);} foreach (int I in SK) {console. writeline (I); // traversal} SK. pop (); console. writeline ("pop"); foreach (int I in SK) {console. writeline (I);} sk2.peek (); // The last item does not delete the console. writeline ("peek"); foreach (int I in sk2) {console. writeline (I);} while (sk2.count! = 0) {int I = (INT) sk2.pop (); // clear sk2.pop (); // clear} console. writeline ("clear"); foreach (int I in sk2) {console. writeline (I );}}

6. Queue class, first-in-first-out. The enqueue method is used to enter the queue, and the dequeue method is used to output the queue.

Class program {static void main (string [] ARGs) {queue Qu = new Queue (); queue qu2 = new Queue (); foreach (int I in new int [4] {1, 2, 3, 4}) {Qu. enqueue (I); // fill in qu2.enqueue (I);} foreach (int I in Qu) {console. writeline (I); // traversal} Qu. dequeue (); console. writeline ("dequeue"); foreach (int I in Qu) {console. writeline (I);} qu2.peek (); // The last item does not delete the console. writeline ("peek"); foreach (int I in qu2 ){ Console. writeline (I);} while (qu2.count! = 0) {int I = (INT) qu2.dequeue (); // clear qu2.dequeue (); // clear} console. writeline ("clear"); foreach (int I in qu2) {console. writeline (I );}}}

 

 

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.