Collection usage analysis in C

  • 2020-11-20 06:12:42
  • OfStack

This article illustrates the use of collections in C# and shares them with you for your reference. The specific analysis is as follows:

A collection, unlike an array, is a combination of a set of variably typed, variably numbered elements that may share certain characteristics and need to be operated on in a certain manner. Generally speaking, in order to facilitate the operation of these elements, the types are the same.

[Difference between a collection and an array: an array is a continuous block of data of the same type, while a collection can be discontinuous and of multiple data types]

[foreach() is also applicable in sets]

1. Definition of set:

ArrayList al = new ArrayList();  // define 1 a   The set, the set is 1 A class, in using System.Collections In the library, you need a reference 

2. Assignment of the set:

double fenshu = 0;

al.Add(fenshu=double.Parse (Console .ReadLine ())); // If the number is saved, the size will be compared in the future. If it needs to be added again, it will be converted to the numeric type first and then added to the collection. Otherwise, it will be used as the encoding of a string to compare the size. 

(You can also use.Add (); al.Add(2); // The data is in brackets. The index number of the first data is 0 by default, and so on.

3. Insert data into the collection:

al.insert( , ); // The comma is preceded by an index number, and the comma is followed by data (when there is data in the collection) 3 And the inserted index number is 1 Is, then it is 1 The data for the index number will be 2 , followed by one after the other 1 A) 

4. Remove the data in the collection:

al.Remove();// The parentheses are for the data to be removed from the collection (if there are two duplicate Numbers in the collection during removal)  .Remove()  Only remove the first 1 Number of occurrences) 

al.RemoveAt();// The brackets contain the index number of the data to be removed from the collection 

5 ·. count; // To view the length of the collection, return type int

6. Sorting in the set:.Sort (); // this is in ascending order. In descending order, use flip (flip --.Reverse ();) after ascending order

7. Find the index number of elements in the collection: (1) Pay attention to whether the data type matches. If the return value is -1, the index number of the element is not found.

int s = al.IndexOf();       // And then the parenthesis is the element you're looking for, the first element 1 Secondary index number 
int s1 = al.LastIndexOf();  // And the parenthesis is the element that you're looking for, the element that comes last 1 Secondary index number

8. Empty the set:.Clear ();

9. Obtain the number of elements in the set:

Console.WriteLine(at.Count);// The number of output sets 

10. Copy the element data in the collection and load it into the new collection:

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

11. Determine whether a collection contains this element data. Return bool value:

bool b = al.Contains(); // In brackets are the elements to find if the collection contains

-- Special set: Stack, Queue, hash table (Hashtable)

Stack heap, last in, first out (heap has no index)

1. Construct Stack s= ES86en. Stack();

2. Assignment: s.Push(1); // Push data into the heap

3. Output: ES94en. WriteLine(s. Pop());

4. Empty the set:.clear();

5. string tanchu = s ().ToString(); // Only gets the last value entered, not removed

string tanchu = s.Pop().ToString(); //Pop is the element that pops out 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

First in, first out, last in, last out

1. Construction: Queue q = new Queue();

2. int chu = int.Parse(q.Dequeue ().ToString ()); // Takes the first entry and removes it from the collection

3. int zhi = int.Parse(q.Peek ().ToString ()); // Read the first entry without removing it

4. bool d = q.Contains(5); // To see if the collection contains parenthesized elements, return the bool value

A hash table (Hashtable) is first in, then out, last in, first out 1 position contains two values (,), preceded by an index and followed by an element

1. Construct Hashtable ht = new Hashtable();

2 · ht. Add (0, "aa"); // Adds a bond value to the hash table

3 · ht. Remove (4); // Remove the Keys value in parentheses

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

5, the output

foreach (int i in ht.Keys)    //Keys Said the index 
{
 Console.WriteLine(i);   // Last in, first out
} foreach (int i in ht..Values)//.Values Indicates that the element
{
 Console.WriteLine(i);   // Last in, first out
}

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

Is:

double fenshu = 0;
0

6. Convert the hash table to Arraylist

ArrayList al = new ArrayList();
foreach (string j in ht.Values )  //Values Represents elements in a hash table
{
 al.Add(j);
}

I hope this article has been helpful for your vc programming.


Related articles: