C # Collections

Source: Internet
Author: User

In the preceding array, the array type must be of the same type, and the specified length

So assuming there are not many variables to know, there are several types of pieces that are particularly troublesome to handle.

Features: Different types of length are not fixed


To use a collection, you must first reference the namespace using System.Collections

Put it in the face.

There are six main categories of collections

Weakly typed ArrayList collection

Strongly typed (generic type) List collection

Hashtable collection (weakly typed) Hashtable collection

Dictionary (Strong) Dictionary collection

Special: Queues Collection Queue set

Trestle Collection Stack Set

First, ArrayList collection

ArrayList is similar to array name, but there are similarities and differences

The same point: You can use an integer index to look for variable elements, including values and assignments , and the index starts at 0 .

It's all unified operation data.

Different points:

The ⑴arraylist is a collection, array is a set;

⑵arraylist is a concrete class, array is an abstract class;

The ⑶ array must be instantiated in the number of elements that once determined that the number cannot be changed, and the type must be the same type

Array collection instantiation does not specify a collection element, the number can be numerous, indeterminate, and can be cut in line, you can specify the initial capacity

⑷ Array Gets the number of elements with length, while the collection gets the number of elements using count

⑸ arrays can be multidimensional, and collections can only be one-dimensional

Defined

ArrayList s=new ArrayList (); (collection type) ArrayList variable =new ArrayList;

S.add (); Variable. Add ( something that is not indexed ) object type: All types of underlying type (base class) int string double bool Datetime ... What types are available

Assign value

ArrayList arr =new ArrayList ();

Arr. ADD (10);

Arr. ADD ("AAA");

Arr. ADD (TRUE);

Arr. ADD (10.3);

Arr. ADD (2017-3-2);

Take value

and arrays are all indexed by value

arr[0]=10; It's all starting from 0.

arr[1]= "AAA";

arr[2]=true;

The number of sets of arr. Count=5;//adds 5 elements to the number is 5

A set of elements in a queue

Arr. Insert (2,333)//The index 2 is inserted in front of an element 333 the original index 2 position to squeeze a bit

Arr. Insert (index, value/variable)

To remove:
Arr. Remove (10.3); Is that the 10.3 in the collection is removed.

Arr.Remove( value );

Arr. Removeat ( index );

Reverse:
Arr. Reverse (); -Reverse All
Arr. Reverse (index, number); -Specify Invert

Empty:
Arr.  Clear (); //Clear the previous data

Contains:

Arr. Contains (value/variable); Determine if a value or variable is inside the collection

The:

Arr. IndexOf (value variable); Looking for a value from a collection, the index of the first occurrence of a variable

Second, the strong Type list collection above the type used is too messy fixed a type

Like what:

Not the same type

System error

List<t>//T means that generics must give it a type

Defined

List<int> a=new list<int> ();

List<string>a=new list<string> ();

list< Basic type > variable =new list< type > ();

Assign value

List<int>a=new list<int> ();

A.add (value); as in the above collection //collection Basically this is the case

Take value

a[Index]= value/variable; It's basically the same thing.

All of the above functions can be used

Third, Hashtable collection Hashtable set weak type

Hashtable collection No indexes can be represented by keys

Do not allow queue jumping

Namespaces

Plus Using ystem.collections

To define a value assignment

Hashtable hs=new Hashtable ();

Hs. ADD (1, "hehe"); 1 for key 1 hehe represents the value

Hs. ADD ("AAA", "haha");// key AAA value is haha

Console.Write (hs[1]);// parentheses can also be written as hs.[" AAA "]

Then the display is hehe

Cyclic value Printing

The Print key is

Printing values is changing the key to values

Display is

Four, Dictionary: Strong type
Dictionary<int, string> dic = new Dictionary<int, string> ();

V. Queue collection queues

Advanced First Out

Defined

Queue a=new queue ();

Assign value

A.enqueque ("AAA"); In the first queue is AAA

A.enqueue (111); Into the second queue is 111

A.dequeque ();//Remove and return the starting object

Console.Write (A.dequeque ());//show AAA means that the person who is the most front shows up and leaves 1.

A.enqueque into the queue

A.dequeque out Queue

A.clear Empty

Vi. Collection of trestle stacks

Advanced after coming out

Stack aaa=new stack ();

Aaa.push ("ASD");//advance to an ASD

Aaa.push (111);//advance to a 111

Console.Write (Aaa.pop ());//display 111 means to pull out the last one (bounce out) 111 and take out 1

C # Collections

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.