Collections in C #

Source: Internet
Author: User

A collection, unlike an array, is a group of mutable types of elements that may share certain characteristics and need to be manipulated in some way. In general, the types of these elements are the same for ease of operation "

"The difference between a collection and an array is that an array is a contiguous area of data of the same type, and the collection can be discontinuous, with multiple data types"

"foreach () is also applicable in the collection"

1 · Definition of the Set: ArrayList al = new ArrayList (); //Define a collection, a collection is a class, in the Using System.Collections Library, you need to reference

2 • Assignment of the set:

Double Fenshu = 0;

Al. ADD (fenshu=double. Parse (Console. ReadLine ())); If it is a number, in the future to compare the size, you need to add the time to convert to a numeric type and then add to the collection, otherwise, as a string encoding to compare size, error!

(can also be used.) ADD (); To assign values such as: AL.  ADD (2); The data is in parentheses. The index number of the first data is 0 by default, followed by an analogy )

3 · Inserting data into the collection:. Insert (,); Before the comma is the index number, after the comma is the data (when there are three data in the collection, the index number is inserted at 1 o'clock, the original 1 index number of data will be 2, followed by backward one bit)

4 • Removes data from the collection:. Remove ();//parentheses fill in the data to be removed in the collection (if there are two duplicate numbers in the collection in the removal.) Remove () removes only the first occurrence of the number)

. RemoveAt ();//The index number of the data to be removed in the set is filled in parentheses

5 .count;//View the length of the collection, return int type

6 · Sorting in the collection:. Sort ();//This is sorted in ascending order, descending order in ascending order after the method is flipped (flipped ———. Reverse ();)

7 · Find the index number of the element in the collection: (Be sure to note whether the data type matches.) If the return value is-1, then the index number of the element is not found)

int s = al.             IndexOf (); In parentheses is the element to be found, the first occurrence of the index number
int S1 = al.      LastIndexOf (); In parentheses is the element to be found, the index number of the last occurrence of this element

8 • Empty the collection:. Clear ();

9 · Gets the number of elements within the collection: Console.WriteLine (at. count);//number of output sets

10 • Copy the element data from the collection and load the new collection:

ArrayList xal = new ArrayList ();
Xal = (ArrayList) al. Clone ();

11 · Determine if a collection contains this element data returns a bool value:

BOOL B = al. Contains ();//in parentheses for the element to find whether the collection contains

————— Special Collection: Stack, Queue, hash table (Hashtable)

Stack heap meaning, advanced out, LIFO (heap no index)

1 • Build the Stack s=new. Stack ();

2 · Assignment: S.       Push (1); Pushing data into the heap

3 • Output: Console.WriteLine (S.pop ());

4 • Clear the collection:. Clear ();

5 · String Tanchu = S.peek (). ToString ();//Only get the last value that goes in, not remove

String Tanchu = S.pop (). ToString ();//pop is the element that pops up and removes the last entry.

6 · Stack Fuzhi = (stack) s.clone ();//Assignment Set

7 · Console.WriteLine (s.count);//Gets the number of elements in the collection

Queue FIFO first, backward after

1 · Build: Queue q = new Queue ();

2 int chu = Int. Parse (Q.dequeue (). ToString ());//Gets the first entry in the element and removes it from the collection

3 int zhi = Int. Parse (Q.peek (). ToString ());//Read the first in element, not remove

4 bool D = Q.contains (5);//See if the collection contains an element in parentheses, return a bool value

Hash table (Hashtable) advanced out, LIFO a position contains two values (,) preceded by an index is an element

1 • Build Hashtable ht = new Hashtable ();

2 · Ht.       ADD (0, "AA"); To add a key value to a hash table

3 ht.  Remove (4); Removed by the keys value in parentheses

4 · Console.WriteLine (HT. Contains (4));//Determine if a key is included

5 · Output

foreach (int i in HT. Keys)//keys represents the index
{
Console.WriteLine (i); Advanced post-out, LIFO
}

foreach (int i in HT: Values)//. Values represents an element
{
Console.WriteLine (i); Advanced post-out, LIFO
}

What if you want to output both the index and the element?

The

Using enumerations to output index numbers and elements
IDictionaryEnumerator IDE = ht. GetEnumerator ();
while (IDE. MoveNext ())
{
Console.WriteLine (IDE. Key + "" +ide. Value);
}

6 · Convert a hash table into ArrayList

ArrayList al = new ArrayList ();
foreach (String J in Ht. Values)//values represents an element in a hash table
{
Al. Add (j);
}

Collections in C #

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.