C# basic ⑥.2-array (bubble sort, find the most value, sort the array, forr reverse)

 

In the previous blog, I shared the enumerations and structures in C#, this time I will share the arrays. Here we mainly focus on the maximization of the array and the sorting of the array.       

One, mind map

                      

 

What is an array?

A set of numbers of the same data type

 

What are the benefits of arrays?

Declare the same type of numbers at once

 

How to declare the four ways of array?

//第一种方式
 int[] num1 = new int[10];             //长度为10,下标为0-9,int类型

//第二种方式
int[] num2 = new int[] { 1,2,3,4,5};   //此数组就存5个值

//第三种方式
int[] num3 = new int[3] { 1, 2, 3 };   //个数和声明数必须一致。此数组就存3个值,值都清楚

//第四种方式
int[] num4 = { 1, 2, 3, 4, 5 };        //简写法

 

How to assign a value to an array?

int[] num1 = new int[10];   //长度为10,下标为0-9,int类型
num1[8] = 20;               //给8这个地方赋个值
Console.WriteLine(num1[8]);                                                
Console.ReadKey();

 For example, if you build a building with 10 floors, if there is an open house, it will look for this room with the 6th floor number 100.    

 


        

2. Actual combat drills

1. After a Chinese test, the teacher asks the class leader to count the scores of each student and calculate the average score of the whole class (a total of 5 students in the class), and then display all the results.

Console.WriteLine("请输入班级人数");                        //用户输入人数
int number = 0;  //平均成绩
int  sum = 0;   //总成绩
 
 if (int.TryParse(Console.ReadLine(), out    number))      //如果用户输入的人数正确
 {
 
      int[] score = new int[number];                       //声明一个数组用来存放成绩
 
      for (int i = 0; i <    score.Length; i+1)
      {
          Console.WriteLine("请输入第{0}个人的成绩", i++);   //用户输入成绩
 
            score[i] = int.Parse(Console.ReadLine());      //把用户输入的成绩转换为int类型并存到数组中
 
             sum += score[i];                              //对分数进行求和 
       }
      Console.WriteLine("总成绩为{0},平均成绩为{1}",sum,sum/number);
 }
 else                                                     //如果用户输入的人数错误
{
       Console.WriteLine("输入有误");
}
Console.ReadKey();

 

2. Find the maximum, minimum, average, and sum of an array

int[] nums =   { 1, 2, 3, 0, 9 };
int    max = int.MinValue;    //求最大值
int    min = int.MaxValue;    //求最小值
int    sum = 0;              //总和初始化为0
 
for (int i = 0; i    < nums.Length ; i++)
 {
    if(nums[i]> max)    //求最大值
    {
        max = nums[i]; 
     }
 
     if(nums[i]< min)    //求最小值
     {
           min = nums[i];
     }
 
      sum += nums[i];     //求和
}
Console.WriteLine("最大值为{0},最小值为{1},总和为{2},平均值为{3}",max,min,sum,sum/nums.Length );
Console.ReadKey();

 

3. Bubble sort

int[] nums = new int[] { 98, 54, 54, 322, 43, 23 };         //一个数组nums,并赋值(要被排序的数组)
int temp = 0;

for (int i = 0; i < nums.Length - 1; i++)                   //循环的轮数,需要6-1=5轮
{
    for (int j = 0; j < nums.Length - 1 - i; j++)           //比较的次数(随着轮数的增加,比较次数减少)
    {
       if (nums[j] > nums[j + 1])                           //相邻两数进行比较,如果前一位>后一位 
        {
            temp = nums[j];                                 //交换位置
            nums[j] = nums[j + 1];
           nums[j + 1] = temp;
        }
    }
}

for (int i = 0; i < nums.Length ; i++)                      //循环输出排序之后的数组元素
{
    Console.Write(nums[i]+"\t");                            //在控制台输出结果,并排序(\t)
}
Console.ReadKey();

 

4. Array sort

Method one, forward output

int[] nums = new int[] { 1, 2, 4, 3, -1 };      //定义一个数组,并赋值
Array.Sort(nums);                               //Array类,对数组元素进行排序
for (int i = 0; i < nums.Length ; i++)          //for循环输出数组元素
{
    Console.Write(nums[i] + "\t");              //在控制台输出内容,注:\t是水平制表符
}
Console.ReadKey();

Method two, reverse output

int[] nums = new int[] { 1, 2, 4, 3, -1 };      //定义一个数组,并赋值
Array.Sort(nums);                               //Array类,对数组元素进行排序
//Array.Reverse(nums);                          //反转,颠倒原来数组,如,颠倒后:-1,3,4,2,1
for (int i = nums.Length - 1; i >= 0; i--)      //倒着输出结果
{
   Console.Write(nums[i] + "\t");
}
Console.ReadKey();

 

5. Use array method calls: calculate the maximum, minimum, average, and sum of all elements of an array

class Program
{
    static void Main(string[] args) 
    {
         Program.ArraySort();                              //调用ArraySort这个方法
         Console.ReadKey();
     }
    
    
    public static void ArraySort()                         //写了一个ArraySort方法,对数组进行排序
    {
       int[] nums = new int[] { 2, 3, 43, 23, 243, 3 };    //定义一个数组,并赋初值
       int max = int.MinValue;                             //最大值
       int min = int.MaxValue;                             //最小值
       int sum = 0;                                        //给求和赋初值为0

       for (int i = 0; i < nums.Length; i++)               //for循环,对数组进行遍历
       {
           if (nums[i] > max)                              //求最大值
           { 
                max = nums[i];                           
           }

           if (nums[i] < min)                              //求最小值
           {
               min = nums[i];
           }
           sum += nums[i];                                 //求和
        }
        Console.WriteLine("最大值为:{0},最小值为:{1}", max, min);             //在控制台输出最大值、最小值
        Console.WriteLine("和为:{0},平均值为:{1}", sum, sum / nums.Length);   //在控制台输出和、平均数
    }
}

 

6. The names of people in the array (Bella, Edward, Jacob, Alice, Carlisle) are divided into: For example: "Bella|Edward..."

string[] names = new string[] { "贝拉", "爱德华", "雅各布", "爱丽丝", "卡莱尔", };     //定义一个字符串变量并赋值
            
string result = "";                                     //定义一个字符串result用来接收加|之后的元素
for(int i = 0; i < names.Length-1; i++)                 //for循环遍历数组元素
{
    result += names[i] + "|";                           //result=names某个元素+|
}
Console.WriteLine(result+names[names.Length -1]);       //在控制台输出结果,最后一个元素不加“|”
Console.ReadKey();

 

7. Each element of an integer array is processed as follows: if the element is positive, the value of the element at this position is +1; if the element is negative, the value of the element at this position is -1; if the element is 0, The same

for (int i = 0; i    < nums.Length; i++)
{
    if(nums[i]>0)                         //如果数>0
    {
        nums[i]++;
    }
 
    if (nums[i] < 0)                      //如果数<0
    {
        nums[i]--;
    }
}
 
for (int i = 0; i < nums.Length; i++)     //循环输出数值
{
    Console.Write(nums[i]+"\t");
}
Console.ReadKey();

 

8.forr: reverse, reverse the original array

Reverse the order of the elements of a string array: {"I", "Yes", "Good people"}{"Good people", "Yes", "I"}. Exchange the i-th and length-i-1th

方法一:Array.Reverse
string[] text = new string[] {"我", "是", "好人"};      //定义一个数组text并赋值
Array.Reverse(text);                                   //反转
for (int i = 0; i < str.Length; i++)                   //遍历数组
{
    Console.Write(str[i]+"\t");                        //在控制台输出结果,注:\t是水平制表符
}
Console.ReadKey();


方法二:forr反转
Array.Sort(text);
for (int i = text.Length  - 1; i >= 0; i--)
{
    Console.Write(text[i] + "\t");
}
Console.ReadKey();

 

9. Array value and assignment

int[] nums = new int[5];              //声明一个数组

for (int i = 0; i < 5; i++)           //赋值
{
    nums[i] = 10;
}

for(int i = 0; i < 5; i++)            //取值
{
    Console.WriteLine(nums[i]);
}
Console.ReadKey();

 

Three, expand

Enumeration: a set of values ​​in a fixed range

Structure: Declare multiple numbers of different data types at once

Array: Declare multiple numbers of the same data type at once

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/108403990