C # Getting Started 2-arrays and collections

Source: Internet
Author: User

Foreword: The array and the collection can put many kinds of data together, greatly simplifies the development process of the program, after a week of learning, is now an array of groups and sets are summarized as follows:

1. Array and set comparison analysis:


Category: Array collection
Length: fixed length indefinite length
Memory: Consumes less memory
The reciprocal conversion of an array must be able to be transformed into a set, whereas the other is not necessarily
Declaration: Data type [] variable name list< data type > variable name;
Assignment: Variable name =new data type
[length]; variable name =new list< data type > ()
READ: variable name. Lenght; variable name. Count;
Modify: Variable name [index]= value; variable name [index]= value;
Initializer: New data type [] {data Value ...} Variable name =new list< data type > () {data value};
To add an element:


Because of the fixed length of the array, solid cannot change the name of the original array length variable. Add (the data to be added);
Insert element: variable name. Insert (index, new data)
Delete element: variable name. RemoveAt (index);
The variable name. Remove (data);

2. Arrays:
Example: Create an array of type int, the variable name is nums, and use the initializer to assign a value to each of its items (optionally assigning some value), let the user enter a subscript and a new number, modify the array corresponding to the subscript of the number, if the subscript out of range, to give the error prompt, and let the user re-enter, The largest number in the output array

int [] Nums=new int[5]{5,8,10,6,7}
Console.Write ("Please enter a subscript")
int Index=int. Parse (Console.ReadLine ());
while (Index < 0 | | index > MAXINDEX)
{
Console.Write ("Input error, please re-enter:");
index = Int. Parse (Console.ReadLine ());
}
Console.Write ("Please enter a new number:");
int newnumber = Int. Parse (Console.ReadLine ());
Nums[index] = Newnumber;
Console.WriteLine;

int max = nums[0];
for (int i = 1; i < Nums. Length; i++)
{
if (Nums[i] > Max)
{
Max = nums[i];//Save this larger number to Max
}
}
Console.WriteLine ("Max number:" + max);

3. Collection:
Example: Using a collection initializer, initializing a collection of type int, then deleting the first item of the collection, adding a number 2 to the first item of the collection, prompting the user to enter 3 numbers, then loading the 3 numbers sequentially into the collection, and finally outputting the contents of the collection

list<int> nums = new List<int> () {3, 6, 8, 2, 1};
Nums. RemoveAt (0);
Nums. Insert (0, 2);

list<int> nums = new list<int> ();
Console.Write ("Please enter the 1th number:");
Nums. ADD (int. Parse (Console.ReadLine ()));
Console.Write ("Please enter the 2nd number:");
Nums. ADD (int. Parse (Console.ReadLine ()));
Console.Write ("Please enter the 3rd number:");
Nums. ADD (int. Parse (Console.ReadLine ()));
for (int i = 0; i < Nums. Count; i++)
{
Console.Write (Nums[i] + "");
}

C # Getting Started 2-arrays and 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.