May 9 Collection

Source: Internet
Author: User

First,//arraylist

ArrayList Myarry = new ArrayList ();

Myarry. Add (1);// adding elements

Myarry. ADD (2);// index also starts from zero

Myarry. ADD (3);

Myarry. ADD (4);

Myarry. ADD (5);

Myarry. Insert (3, "7");

Insert a value of 7 at the position of index number 3

The subsequent index is sequentially backwards +1

Myarry. Remove (4);

Remove The element with data 4

inside the parentheses is the data you want to remove

Myarry. RemoveAt (4);

Remove data with index number 4

int aa = Myarry. Count;

Count the number of elements in the collection

Console.WriteLine ("A total of " +aa+ " elements " );

Myarry. Clear ();// Clear the collection

BOOL B = Myarry. Contains (3);

determines whether the data in parentheses is returned with a bool value (True or False)

int bb = Myarry. IndexOf (2);

int cc = Myarry. LastIndexOf (2);

Console.WriteLine (BB);

Myarry. Sort ();

automatic sorting, ascending

If you need to sort in descending order, reverse the entire set after automatic arrangement

Myarry. Reverse ();

ArrayList ar = new ArrayList ();

AR = (ArrayList) myarry. Clone ();

clones of collections

foreach (Object A in Myarry)

{// print out all elements in the collection from beginning to end

Console.WriteLine (a);

}

Console.ReadLine ();

There are elements in the collection that are present in the type object

The object class is the base class for all classes

small data types can not receive Data of type object

a variable of type object can receive any type of variable

String AAA = "2";

Object BBB = AAA;

AAA can not receive The value of BBB

Console.WriteLine (Myarry[3]);

Console.ReadLine ();

Stack AA = new stack ();

Aa. Push (1);// Add Data to the stack collection

Aa. Push (2);

Aa. Push (3);

Aa. Push (4);

Aa. Push (5);

Note:theStack is not indexed

Console.WriteLine (aa[0]);// is wrong

Console.WriteLine (AA. Peek ());// only used to view the last one, do not kick out

Aa. Pop ();// eject, kick out the last data into the collection

Console.WriteLine (AA. Pop ());

int AAA = AA. Number of count;// statistics

Aa. Clear ();// emptying the collection

foreach (Object A in AA)

//{

Console.WriteLine (a);

//}

Console.ReadLine ();

ArrayList Collection

Define a collection, initialize

ArrayList al = new ArrayList ();

Al. Add (1);//Adds an element, appended to the end of the collection

Al. ADD (2);

Al. ADD (3);

Al. ADD (4);

Console.WriteLine (Al[1]);

Al. Insert (1, 9);//inserting a value on an index number

Once inserted, the original index number on index 1th is top to the next, and so on

Console.WriteLine (Al[1]);

Al. Remove (4);//Here is a value, remove the value, and remove the first value from the Go

Al. RemoveAt (3);//Remove the value of index number 3rd

Define number of Receive

int Geshu = al. Count;

Console.WriteLine (Geshu);

Al. Clear ();//Emptying the collection

BOOL B = al. Contains (3);//Determine if the data in parentheses is returned with a bool value (TRUE or FALSE)

Console.WriteLine (b);

Al. Sort ();//automatic sorting, ascending

Al. Reverse ();//Flip the collection, usually after sort, and then reverse, into descending order

Clone a set of one-touch

ArrayList AAL = new ArrayList ();

AAL = (ArrayList) al. Clone ();//(ArrayList) al. Clone (); This means putting al. Clone (); cast to ArrayList and assigned to AAL

Iterating through the collection

foreach (Object A in AL)

//{

Console.WriteLine (a);

//}

The object class is the base class for all data types

int a = 1;

Object B = A;

Object c = 3;

A = (int) c;

String d = "ABCD";

Object E = D;

Practice one, enter the number of people, input scores, stored in the collection, and then read out, to find the average score

ArrayList al = new ArrayList ();

Console.Write ("Please enter class Number:");

int n = Int. Parse (Console.ReadLine ());

Double sum = 0;

for (int i = 0; i < n; i++)

//{

Console.Write ("Please enter the section" + (I+1) + "score:");

Al. ADD (double. Parse (Console.ReadLine ()));

Sum + = Double. Parse (Al[i]. ToString ());

//}

Console.WriteLine ("Average divided into:" + (sum/n));

Console.ReadLine ();

Second, Queue Collection

Advanced first-out, backward-out

No index

Queue QQ = new Queue ();

Sql Enqueue (1);// add elements to the collection

Sql Enqueue (2);

Sql Enqueue (3);

Sql Enqueue (4);

Sql Enqueue (5);

Sql Dequeue ();// will be ranked at the front of the culling

int C =qq. Number of count;// statistics

Sql Peek ();// View only, do not kick out

Sql Clear ();// emptying the collection

BOOL BB =qq. Contains (3);// determine if this element is included

foreach (object B in QQ)

{

Console.WriteLine (b);

}

Console.ReadLine ();

May 9 Collection

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.