C # Programming--arrays

Source: Internet
Author: User
Tags array definition array sort null null

Array

You can help me. We declare the storage of multiple variables of the same type at a time.
functions to solve the same class of large amounts of data in memory storage and operation
Features: Continuous, same type of data
Array definition ==> Assignment ==> value
Defined:
int[] Nums0 = new INT[10];//10 is the length of the array
Int[] Nums1 = {1, 2, 3, 4, 5, 6, 7};
int[] Nums2 = new int[] {1, 2, 3};//not recommended for trouble
Int[] Nums3=new int[]{1,2,3,4,5,6,};//not recommended
Assignment: array name [subscript]= value;
Value: array name [subscript value]//subscript starting from 0, the maximum subscript is 1 smaller than the array length.
The benefits of arrays:
1, for a large amount of data, save the time to define an array can be resolved
2, using the loop to control the array subscript, can be used to batch operation of the group
Definition: Specify type, specified length, specified name
Category: One-dimensional arrays, two-dimensional arrays, multidimensional arrays

One-dimensional array: beans

Syntax: array type [] Array name = new array type [array length];

Note: In memory to open up contiguous n blocks of space, each piece is called an array of elements, if you want to access an array of elements, you need to pass the element's subscript or index to access. (Subscript {0~m}, with m-1 elements)

Assigning and taking values of arrays

Once the array length is fixed, it can no longer be changed.

Save multiple values; Almost any type can declare an array; int[] Nums = new int[3];int[] Nums = {5,3,8};
In[]nums=new int[3]{1,2,3};int[]nums=new int[]{2,3,5};
Declaration of the array: above four kinds
int[] Nums = new int[3]{5,3,8}//must be the same number and number of declarations
int[] nums = new int[5]{5,3,8}//error
int[] Nums = new int[]{5,3,8}//correct, can omit array of words
Access the elements of the array by using the indexer to access the element of the specified number position: Nums[0], nums[1]. The index starts at 0. The type of the element to be taken is the type of the array element. You can also assign a value to an array element

#regionArray Basics Learningint[] A =New int[5]; a[0] =1; a[1] =2; a[2] =3; a[3] =4; a[4] =5; Console.WriteLine (a[3]); //Array type [] Array name = new array type [array length];            int[] Nums =New int[Ten];//The initial value is "" Null            string[] str =New string[Ten];//The difference between null and "" String type default array value is null NULL not in memory open space, "": Empty in memory open space            BOOL[] Bools =New BOOL[Ten];//the initial value is Falsenums[0] =1; nums[1] =2; nums[2] =3; nums[6] =Ten;//An element with subscript 6 is assigned a value of 10, which is the 7th element in the array (the subscript starts at 0 and all its subscripts are 6)known number of cycles assigning values to an array with A for loop for(inti =0; I < Nums. Length; i++) {Nums[i]=i; }            //and then, by a cyclic array of values             for(inti =0; I < Nums. Length; i++) {Console.WriteLine (nums[i]); }            //How to declare: the first two ways of recommending            int[] Nums0 =New int[Ten]; int[] Nums1 = {1,2,3,4,5,6,7 }; int[] Nums2 =New int[] {1,2,3};//not recommended more trouble            int[] nums3=New int[]{1,2,3,4,5,6,}; #endregion

FAQs : Averaging, finding the maximum, summing, sorting
Array sort and reverse order (example:)

#regionArrayint[] Nums = {9,8,7,6,5,4,3,2,1,0 }; Array.Sort (nums);//You can only do an ascending order for the arrayArray.reverse (Nums);//reverses the array. With Array.Sort can be implemented in descending order//for (int i = 0; i < Nums. Length-1; i++)//{            //For (int j = 0; J < Nums. Length-1-i; J + +)//    {            //if (nums[j]>nums[j+1])//change to less than: descending//        {            //int temp = nums[j]; //Nums[j] = nums[j + 1]; //nums[j + 1] = temp; //        }            //    }            //}             for(inti =0; I < Nums. Length; i++) {Console.WriteLine (nums[i]);            } console.readkey (); #endregion


Bubble Sort :
Sorts the elements in an array in order from large to small or from small to large
Int[] nums={9,8,7,6,5,4,3,2,1,0}

Number of trips and times:
A maximum (minimum) value per trip,

Number of times: n-1
Number of times: N-sailings

#regionBubble sortint[] A = {423,234, the,343,653,231,452 }; //elements in the output array: The output is the way it was before the sortConsole.WriteLine ("Original array element:");  for(inti =0; i < a.length; i++) {Console.WriteLine (a[i]); }            //use a double for loop to sort and output (for example: from small to large)Console.WriteLine ("sort by from small to large:");  for(inti =1; I <= a.length; i++)//How many trips the outer layer compares            {                 for(intj =1; J <= A.length-i; J + +)//How many times are the inner layers compared?                {                    if(A[j-1] > A[j])//if the first array element is greater than the second array element, the                    {                        inttemp = a[j-1]; A[j-1] =A[j]; A[J]=temp; }                }            }             for(inti =0; i < a.length; i++)//output An array of arrays that are already lined up{Console.WriteLine (a[i]); }            #endregion

Two-point query
The premise: The array must be ordered.

Two-dimensional arrays

Syntax: ... Slightly more ...
Equivalent to a table
Can do simple push box

Multidimensional arrays


······ Slightly more ...

C # Programming--arrays

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.