C # set,

Source: Internet
Author: User

C # set,

 

ArrayList
ArrayList myarry = new ArrayList ();
Myarry. Add (1); // Add an element
Myarry. Add (2); // The index also starts from scratch.
Myarry. Add (3 );
Myarry. Add (4 );
Myarry. Add (5 );

Myarry. Insert (3, "7 ");
Insert a value of 7 at the position where index number is 3
The following indexes are sequentially backward + 1

Myarry. Remove (4 );
Remove elements with data 4
The data to be removed is in the brackets.

Myarry. RemoveAt (4 );
Remove Data with index 4

Int aa = myarry. Count;
Count the number of elements in a set
Console. WriteLine ("Total" + aa + "elements ");

Myarry. Clear (); // Clear the set

Bool B = myarry. Contains (3 );
Determines whether data in parentheses exists. The returned 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 want to sort in descending order, the entire set is reversed after the automatic arrangement.
Myarry. Reverse ();

ArrayList ar = new ArrayList ();
Ar = (ArrayList) myarry. Clone ();
Clone a set

Foreach (object a in myarry)
{// Print all elements in the set from start to end
Console. WriteLine ();
}

Console. ReadLine ();

 

Elements in the set exist as objects.
Object class is the base class of all classes.
Small Data Types cannot receive object-type data
Object-type variables can receive any type of Variables
String aaa = "2 ";
Object bbb = aaa;
Aaa cannot receive bbb values

Console. WriteLine (myarry [3]);
Console. ReadLine ();

 

Stack set

Stack aa = new Stack ();
Aa. Push (1); // Add data to the stack set
Aa. Push (2 );
Aa. Push (3 );
Aa. Push (4 );
Aa. Push (5 );

Note: The Stack is not indexed.
Console. WriteLine (aa [0]); // the error message returned.

Console. WriteLine (aa. Peek (); // It is only used to view the last digit and is not kicked out.

Aa. Pop (); // Pop up, kicking out the data from the last entry to the set

Console. WriteLine (aa. Pop ());

Int aaa = aa. Count; // Count

Aa. Clear (); // Clear the set


Foreach (object a in aa)
{
Console. WriteLine ();
}

Console. ReadLine ();


Queue set
First-in, second-in, and second-out
No Index
Queue qq = new Queue ();
Qq. Enqueue (1); // add elements to the set
Qq. Enqueue (2 );
Qq. Enqueue (3 );
Qq. Enqueue (4 );
Qq. Enqueue (5 );

Qq. Dequeue (); // removes the top one
Int c = qq. Count; // Count

Qq. Peek (); // view only, do not kick out

Qq. Clear (); // Clear the set
Bool bb = qq. Contains (3); // determines whether the element is contained.

Foreach (object B in qq)
{
Console. WriteLine (B );
}

Console. ReadLine ();

Foreach traversal set
Foreach (object aa in name)
{
Console. Write (aa + "\ t ");
}

Console. ReadLine ();

Object;
Int B = 4;
A = B;
Double c = 3.14;
A = c;
Bool d = true;
A = d;
String e = "Hello ";
A = e;
DateTime dt = DateTime. Now;
A = dt;

After-school questions
Enter the number of students in the class, and enter the name and score of each person to enter the set.
Calculate the total score of Chinese and the average score of mathematics,
Name of the person with the highest score in English and scores of all subjects (bubble sort)
0 name 4 8 12
1 Language score 5 9 13
2. Mathematical score 6 10 14
3. English score 7 11 15
Console. Write ("Enter the number of students in the class :");
Int n = int. Parse (Console. ReadLine ());
Double yu = 0;
Double shu = 0;
ArrayList al = new ArrayList ();
For (int I = 0; I <n; I ++)
{
Console. Write ("enter the name of the {0} person:", I + 1 );
Al. Add (Console. ReadLine ());
Console. Write ("Enter the language score of {0} students:", I + 1 );
Al. Add (double. Parse (Console. ReadLine ()));
Yu + = double. Parse (al [al. Count-1]. ToString ());
Console. Write ("Enter the {0} student's mathematical score:", I + 1 );
Al. Add (double. Parse (Console. ReadLine ()));
Shu + = double. Parse (al [al. Count-1]. ToString ());
Console. Write ("Enter the English score of the {0} student (s):", I + 1 );
Al. Add (double. Parse (Console. ReadLine ()));
}
Console. WriteLine ("after entering all information, press enter to continue! ");
Console. ReadLine ();
Console. WriteLine ("total Chinese score:" + yu );
Console. WriteLine ("the average mathematical score is:" + (shu/n ));

For (int I = 3; I <4 * n-1; I + = 4)
{
For (int j = I; j <4 * n-4; j + = 4)
{
If (double. Parse (al [I]. ToString () <double. Parse (al [j + 4]. ToString ()))
{
Change name
Object zhong = al [I-3];
Al [I-3] = al [j + 1];
Al [j + 1] = zhong;
Change Language
Zhong = al [I-2];
Al [I-2] = al [j + 2];
Al [j + 2] = zhong;
Change to mathematics
Zhong = al [I-1];
Al [I-1] = al [j + 3];
Al [j + 3] = zhong;
Change to English
Zhong = al [I];
Al [I] = al [j + 4];
Al [j + 4] = zhong;
}
}
}
Console. writeLine ("students with the highest English score are {0}, English scores are {1}, Chinese scores are {2}, and mathematics scores are {3}", al [0], al [3], al [1], al [2]);
Console. ReadLine ();

Related Article

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.