C#_ Basic _ Array (ix)

Source: Internet
Author: User
Tags array length

Concept: Storing multiple variables of the same type at once, distinguishing structures from declaring multiple variables of different types at once

1. Dynamic initialization of arrays

   // Array type [] Array name = new array type [array length];    int New int [+// Declaration of an int type, an array of length 10

The default is 0 in the array

Assigning values to an array:

1 nums[5] = ten;

2 nums[3] = n;

2. Static initialization

1 int[] num2 = {1,2,3 };2 //The following two kinds of can, but more complex than the second, generally used in first and second,3//the third type of incoming array length corresponds to the number of data in the curly braces that follow4  int[] num3 =New int[3] {1,2,3 };5  int[] Num4 =New int[] {1,2,3};

1   //Exercise 1: Remove the largest integer, smallest integer, sum, average, from an array of integers2                          //Note: 0 is the case of a maximum or minimum value, assigning an initial value to Max to an element in the array int3 4                          intmax = nums[0];5                          intMin = nums[0];6                                 //either the maximum and minimum values of the assignment int type, giving the maximum a minimum, a minimum value, and a maximum7Max =int. MinValue;8Min =int. MaxValue;9 Ten                          intsum =0; One                          intarg =0; A                           for(inti =0; I < Nums. Length; i++) -                          { -                              if(Max <Nums[i]) the                              { -Max =Nums[i]; -                              } -  +min = min > nums[i]?Min:nums[i]; -  +Sum + =Nums[i]; A                          } at  -Console.WriteLine ("The maximum number is {0}, the total is {1}, and the average is {2:0.00}", Max, SUM, sum *1.0/Nums. Length);
Exercises
1 //Bubble Sort: Sorts the data in an array from large to small or from smallest to largest2                     //procedure: The first element and the next element are compared next to each other, and if large, the Exchange3                     //from front to back, get the last value is the maximum value4                          int[] arr = { A,4,5,7, +, +, at, -, at };5                           for(inti =0; I < arr. length-1; i++)6                          {7                               for(intj =0; J < arr. length-1-I.; J + +)8                              {9                                  if(Arr[j] > arr[j+1])Ten                                  { One                                      inttemp =Arr[j]; AARR[J] = arr[j+1]; -arr[j+1] =temp; -                                  } the                              } -                          } -                        //Array.Sort (arr); //Ascending order -                        //Array.reverse (arr);//Array to flip +                           for(inti =0; I < arr. Length; i++) -                          { +Console.WriteLine ("arr[{0}] = {1}", I, Arr[i]); A}
Bubble Sort Exercise

Note: The array index exceeds the array length, throws an exception, indexes out of bounds beyond array bound,

C#_ Basic _ Array (ix)

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.