Collections and Generic Collections

Source: Internet
Author: User

Collection:
As a case, there is no fixed length, you can put any type of data or variables;
The collection also has an index;

ArrayList:
Defined:
ArrayList arr = new ArrayList ();
To add an element:
Arr. Add (a);
Arr. ADD (b);

Value:
arr[index];
Note that the value is an object type and requires a type conversion;

Note: A collection can be any type, but when we use a set, we usually put a type;

Special Features:
Arr. ADD (); adding elements
Arr. Insert (index, value); Jump in queue, jump the value into the index position
Arr. Remove (value); Remove elements from a collection based on values
Arr. RemoveAt (index); To delete an element in a collection based on an index
Arr. Clear (); Emptying the collection

Arr. Sort (); Arranges all elements in a collection from small to large
Arr. Reverse ();
Arr. Contains (); To see if an element is contained in the collection
Arr.count; The number of elements in the collection, returning an int type

-------------------------------------------------------------
Exercises:
Please enter the number of students:
Please enter the name of the 1th student:
Please enter the 1th student's gender:
Please enter a 1th student's age:
Please enter the 1th student's grade:
...
N
********************************
1th Student's name: XXX, Gender: xxx, Age: XXX, score: XXX.
...
N

Practice Questions: Please enter the number of students: Please enter the 1th student's name: Please enter the 1th student's gender: Please enter the age of the 1th student: Please enter the 1th student's grade: ... n********************************1th Student's name: XXX, Gender: xxx, Age: XXX, score: XXX. ... n Console.Write ("Please enter the number of students:"); intGS =Convert.ToInt32 (Console.ReadLine ()); ArrayList a=NewArrayList (); ArrayList b=NewArrayList (); ArrayList C=NewArrayList (); ArrayList D=NewArrayList (); decimalEnd =0m;  for(inti =0; I < GS; i++) {Console.Write ("Please enter section"+ (i +1) +"name of student:"); stringXM =Console.ReadLine (); Console.Write ("Please enter section"+ (i +1) +"Gender of Student:"); stringXB =Console.ReadLine (); Console.Write ("Please enter section"+ (i +1) +"age of Student:"); intNL =Convert.ToInt32 (Console.ReadLine ()); Console.Write ("Please enter section"+ (i +1) +"Student Results:"); decimalCJ =Convert.todecimal (Console.ReadLine ());                A.add (XM);                B.add (XB);                C.add (NL);                D.add (CJ); End+=CJ; } Console.WriteLine ("**************************************************");  for(intj =0; J < GS; J + +) {Console.WriteLine ("Section"+ (J +1) +"the name of a student:"+ A[j] +", Gender:"+ B[j] +", Age:"+ C[j] +", Results:"+ D[j] +". "); } Console.WriteLine ("The total score is:"+ END +"The average score is:"+ (END/GS) +". "); Console.readkey ();


-------------------------------------------------------------
Special Collections:

Queue Queues Collection
Defined:
Queue QQ = new Queue ();
To add an element:
Enqueue ("value"/variable);
Value:
Dequeue (); Takes the first entry value and removes it from the collection.
Peek (); Take the first entry value to show you that it will not be deleted in the collection
-------------------------------------------------------------
Stack Trestle Collection
Defined:
Stack ss = new stack ();
To add an element:
Ss. Push ("value"/variable);
Value:
Ss. Pop (); Advanced go out, take the last one in, and delete in the collection
Ss. Peek (); Take the last one in, show it to you, and not remove it from the collection.
-------------------------------------------------------------
Hashtable: Hashtable Collection
Key-value pairs, one for the corresponding value;
Defined:
Hashtable ha = new Hashtable ();
To add an element:
ADD (Key,value); Key, user-defined index, object type
Value that corresponds to the current index
Value:
Ha[key]; Remove the corresponding content according to user-defined index

Traverse:
foreach (String i in Ha. Keys)//You can traverse the keys individually
{
Console.WriteLine (i);
}

foreach (String i in Ha. Values)//The value can be traversed independently
{
Console.WriteLine (i);
}
-------------------------------------------------------------
Generic collection:
list< type > li = new list< type > ();

Li. ADD (a value or variable of the same type);

li[Index]

Collections and Generic 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.