zoukankan      html  css  js  c++  java
  • C#的结构和数组

    下面我们继续学习C#的语法。结构struct,C#中的结构和我们PLC中建立的UDT(结构体)是一样的。里面存储了相关的不同类型的数据。

    有一句话我觉得十分重要:方法是依存于结构和对象存在的这以后我们会个更加深入的学习的。

    Struct结构:

    可以帮助我们一次性声明不同类型的变量。

    语法:

    [public] struct 结构名

    {

        成员;

    }

    如下例声明:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         public struct Person
    12         {
    13             public string name;
    14             public int age;
    15             public char gender;
    16         }
    17         static void Main(string[] args)
    18         {
    19             Person zsPerson;
    20             zsPerson.name = "张三";
    21             zsPerson.age = 18;
    22             zsPerson.gender = '';
    23 
    24             Console.ReadKey();
    25         }
    26     }
    27 }

    值得我们注意的是,在声明结构的时候,如果我们没加public,我们是建立不了给结构赋值的,不加public系统默认为private私有的。并且我们在命名空间之下Main之上创建的变量其实不叫变量,而是叫字段。

    变量和字段的区别在于:变量可以存一个值,之后不停被覆盖,而字段类似我们PLC背景数据,可以存储若干个数值。

    而且我在这要提出一个问题,我看了几个视频和数据,对于字段的命名说法不一样的,总结如下

    (1)字段和变量要区别命名,例如:_Name

    (2)也有反对这种命名方式的,理由是:在复杂的编程任务中,可能影响与其他语言的交互引用的作用,例如VB。net。

    这在以后深入学习过程中我们在慢慢体会,也欢迎大神们给我解惑。


    数组

    一次性存储多个相同类型的变量。

    语法:

    数组的类型[] 数组名 = new 数组类型[数组长度];

    数组的长度一旦固定了,就不能在被改变了。

    对于int[]类型的数组,初值为0,string[]数组初值为null,bool[]数组初值为false。

    下面我们介绍几种声明数组的方式

    int[] nums = new int[10]; //没有声明数组元素,推荐

    int[] nums = {1,2,3,4,5,6}; //隐式声明了元素和长度,推荐

    int[] nums = new int[3]{1,2,3};  //不推荐,麻烦且长度和元素数量必须一致。

    int[] nums = new int[]{1,2,3,4,5};  //类似第2种

    下面看一个练习1:从一个整数数组中求出最大值,最小值,总和和平均值。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int[] nums = { 1,2,3,4,5,6,7,8,9,0};
    14             int max = nums[0];
    15             int min = nums[0];
    16             int sum = 0;
    17 
    18             for (int i = 0; i < nums.Length; i++)
    19             {
    20                 if (nums[i] > max)
    21                 {
    22                     max = nums[i];
    23                 }
    24 
    25                 if (nums[i] < min)
    26                 {
    27                     min = nums[i];
    28                 }
    29                 sum += nums[i];
    30             }
    31             Console.WriteLine($"这个数组的最大值是{max},最小值是{min},总和是{sum},平均值是{sum/nums.Length}");
    32             Console.ReadKey();
    33         }
    34     }
    35 }

    练习2:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string[] names = { "老杨","老苏","老邹","老虎","老牛","老马"};
    14             string str = null;
    15 
    16             for (int i = 0; i < names.Length-1; i++)
    17             {
    18                 str += names[i] + "|";
    19             }
    20             Console.WriteLine(str+names[names.Length-1]);
    21             Console.ReadKey();
    22         }
    23     }
    24 }

    练习3:对一个整数数组做如下处理:若元素为正数将这个元素+1,若为负数,将这个元素-1,元素为0,不变。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int[] nums = { 1,-2,3,-4,5,6,0};
    14             for (int i = 0; i < nums.Length; i++)
    15             {
    16                 if (nums[i] > 0)
    17                 {
    18                     nums[i] += 1;
    19                 }
    20                 else if (nums[i] < 0)
    21                 {
    22                     nums[i] -= 1;
    23                 }
    24                 else
    25                 {
    26 
    27                 }
    28             }
    29 
    30             for (int i = 0; i < nums.Length; i++)
    31             {
    32                 Console.WriteLine(nums[i]);
    33             }
    34             Console.ReadKey();
    35         }
    36     }
    37 }

    练习4:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string[] names = { "","","好人"};
    14             for (int i = 0; i < names.Length/2; i++)
    15             {
    16                 string temp = names[i];
    17                 names[i] = names[names.Length - 1 - i];
    18                 names[names.Length - 1 - i] = temp;
    19             }
    20             for (int i = 0; i < names.Length; i++)
    21             {
    22                 Console.Write(names[i]);
    23             }
    24             Console.ReadKey();
    25         }
    26     }
    27 }

    练习5:冒泡排序:就是将一个数组中的元素从大到小,从小到大排列。

           分析:需要两个循环,外层循环,控制比较次数,内层循环,控制交换次数。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int[] nums = { 9,8,7,6,5,4,3,2,1,0};
    14             for (int i = 0; i < nums.Length-1; i++)
    15             {
    16                 for (int j = 0; j < nums.Length-1-i; j++)
    17                 {
    18                     if (nums[j] > nums[j+1])
    19                     {
    20                         int temp = nums[j];
    21                         nums[j] = nums[j + 1];
    22                         nums[j + 1] = temp;
    23                     }
    24                 }
    25             }
    26             for (int i = 0; i < nums.Length; i++)
    27             {
    28                 Console.WriteLine(nums[i]);
    29             }
    30             Console.ReadKey();
    31         }
    32     }
    33 }

    这里面有一点值得我们注意,C#中的数组下标和我们PLC中数组下标正好相反,C#中数组下标的0从左面元素开始计算。

    其实,这种冒泡方式的写法也就在面试的时候会用到,在我们C#中,可以直接用一个方法解决Array.Sort();(只能升序)

    Array.Reverse();(反转排列)若想降序:先调用Array.Sort();后调用Array.Reverse()。

  • 相关阅读:
    Topo软件
    如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
    Web学习
    测试Web服务接口
    WebService
    记录几个博客
    Oracle日志性能查看
    设计模式
    hdu 1999 不可摸数
    parfor —— matlab 下的并行循环
  • 原文地址:https://www.cnblogs.com/LiyuLi/p/12097252.html
Copyright © 2011-2022 走看看