C#的結構和數組

下面咱們繼續學習C#的語法。結構struct,C#中的結構和咱們PLC中創建的UDT(結構體)是同樣的。裏面存儲了相關的不一樣類型的數據。面試

有一句話我以爲十分重要:方法是依存於結構和對象存在的這之後咱們會個更加深刻的學習的。編程

Struct結構:數組

能夠幫助咱們一次性聲明不一樣類型的變量。學習

語法:spa

[public] struct 結構名code

{視頻

    成員;對象

}blog

以下例聲明:排序

 

 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()。

相關文章
相關標籤/搜索