C# 基礎Array


一、Array的作用

  連續定義多個相同類型的變量,比如我定義1000個學生的學生年齡,int[] age = new int[1000];不需要慢慢的一個一個變量的定義,數組是不是很方便。

需要注意的:1)Array是引用類型。引用類型是傳遞棧中的地址。2)數組都是由連續的內存位置組成的。

二、Array定義與賦值

1)先定義再賦值比如:

 

 1             //定義100個學生的成績
 2             //第一種定義並初始化方式
 3             int[] studentSorce = new int[100];
 4             studentSorce[0] = 78;
 5             studentSorce[1] = 95;
 6             //第二種定義並初始化方式
 7             int[] studentSorce02 = new int[5]{78,95,84,76,59};
 8             //第三種定義並初始化方式
 9             int[] studentSorce03 = new int[] { 78, 95, 84, 76, 59 };
10             //第四種定義並初始化方式
11             int[] studentSorce04 = { 78, 95, 84, 76, 59 };
12             //二維定義方式
13             int[,] arr = new int[3, 4];
14             //二維定義並賦值,3行4列
15             int[,] arr02 = new int[3, 4] { { 75,96,83,65 }, { 78, 95, 84, 76 },{86,66,73,77} };

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 ArrayTest
 8 {
 9 
10     class Program
11     {
12         public static int[,] array2D;        //定義二維數組,沒賦值數組長度
13 
14         /// <summary>
15         /// 隨機生成二維數組
16         /// </summary>
17         /// <param name="rows">一維(行)</param>
18         /// <param name="columns">二維(列)</param>
19         public static void RaArray2D(int rows, int columns)
20         {
21             Random ra = new Random();
22             array2D = new int[rows, columns];
23             for (int i = 0; i < array2D.GetLength(0); i++)
24             {
25                 for (int j = 0; j < array2D.GetLength(1); j++)
26                 {
27                     array2D[i, j] = ra.Next(10, 101); //范圍10-100的隨機數
28                 }
29             }
30         }
31         /// <summary>
32         /// 遍歷二維數組
33         /// </summary>
34         /// <param name="arr2D">二維數組</param>
35         public static void EachArray2D(int[,] arr2D)
36         {
37             for (int i = 0; i < arr2D.GetLength(0); i++)
38             {
39                 for (int j = 0; j < arr2D.GetLength(1); j++)
40                 {
41                     Console.Write(arr2D[i, j].ToString().PadRight(4, '\u0000'));
42                 }
43                 Console.WriteLine();
44             }
45         }
46 
47         static void Main(string[] args)
48         {
49             RaArray2D(5, 10);
50             EachArray2D(array2D);
51 
52             Console.WriteLine(array2D.Length);//顯示二維數組總長度.
53             Console.WriteLine(array2D.Rank);    //數組維度
54 
55             Console.ReadKey();
56 
57         }
58 
59     }
60 }

 

 

 

 

 

3)數組初始化的值,代碼 int[] studentSorce = new int[5]; 我們進行兩步創建。

 

            int[] x;            //定義int[]的數組,沒有賦值.
            x = new int[5];     //賦值后x[0]...x[4]初值0

這樣我們就可以在類中定義數組,然后在方法中賦值。如下

 

public static int[] newArr;   //在類中定義,然后在方法中賦值。估計可行。

 

三、Array常用方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ArrayTest
 8 {
 9 
10     class Program
11     {
12         public static int[] array1D;        //聲明array1D變量,該變量的類型為int[]
13         /// <summary>
14         /// 隨機生成一維數組
15         /// </summary>
16         /// <param name="number">表示數組的長度</param>
17         public static void RaArray(int number)
18         {
19             array1D = new int[number];      //創建了一個數組,將數組的地址賦值給array1D
20             Random ra = new Random();
21             for (int i = 0; i < array1D.Length; i++)
22             {
23                 array1D[i] = ra.Next(30, 101); //隨機范圍30-100
24             }
25         }
26         /// <summary>
27         /// 遍歷數組方法
28         /// </summary>
29         public static void EachArray()
30         {
31             for (int i = 0; i < array1D.Length; i++)
32             {
33                 Console.Write(array1D[i].ToString().PadRight(4, '\u0000'));
34             }
35         }
36 
37 
38         static void Main(string[] args)
39         {
40             RaArray(10);    //生成隨機數組方法
41             EachArray();    //遍歷數組的方法
42             //下面介紹一些數組的方法
43             Console.WriteLine();    //換行
44             Array.Sort(array1D);    //排序由小到大
45             EachArray();
46             Console.WriteLine();    //換行
47             Array.Reverse(array1D); //數組元素反轉
48             EachArray();
49             Console.WriteLine();    //換行
50             int max = array1D.Max();
51             int min = array1D.Min();
52             int sum = array1D.Sum();
53             double avg = array1D.Average();
54             Console.WriteLine("array1D數組的最大值:{0},最小值:{1},和:{2},平均值:{3}", max, min, sum, avg);
55             Console.WriteLine();    //換行
56             Console.WriteLine("輸入要查詢的數字");
57             int chaXun = int.Parse(Console.ReadLine());
58             bool isContains = array1D.Contains(chaXun); //查詢數組是否包含指定數,返回bool
59             Console.WriteLine(isContains);
60             Console.WriteLine();
61             int[] newArray1d = new int[array1D.Length];
62             array1D.CopyTo(newArray1d, 0); //將array1D的所有元素復制給newArray1D
63             for (int i = 0; i < newArray1d.Length; i++)
64             {
65                 Console.Write(newArray1d[i].ToString().PadRight(4, '\u0000'));
66             }
67             Console.WriteLine("輸入數字,檢索第一次出現的索引值");
68             int number = int.Parse(Console.ReadLine());
69             int firstIndex = Array.IndexOf(array1D, number); //搜索array1D中第一次出現指定值的索引值
70             int laseIndex = Array.LastIndexOf(array1D, number); //最后一次出現的索引值.
71 
72             Console.WriteLine("搜索第一次出現{0}位置索引號為:{1},最后一次出現的索引號為{2}", number, firstIndex, laseIndex);
73 
74             Console.WriteLine(Array.Find(array1D, item => item > 5)); //數組中第一個大於5的數字
75             Console.WriteLine(Array.FindLast(array1D, items => items < 90)); //檢索數組最后一個大於90的值並輸出
76             foreach (int item in Array.FindAll(array1D,item=>item>50)) //遍歷所有大於50的值
77             {
78                 Console.Write("大於50的有{0}", item); 
79             }
80             Console.WriteLine(); //換行
81             //第一個大於50的索引值,和最后一個大於50的索引值
82             Console.WriteLine("第一個大於50的索引是:{0},最后大於50的是{1}", Array.FindIndex(array1D, item => item > 50), Array.FindLastIndex(array1D, item => item > 50));
83             Console.ReadKey();
84         }
85 
86     }
87 }

 

 

 

四、交錯數組

 交錯數組:每行元素個數不想等的二維數組。有時也稱“數組的數組”定義、初始化、遍歷如下代碼:

 1             //交錯數組,定義與初始化
 2             int[][] interleavedArray = new int[4][];
 3             interleavedArray[0] = new int[4] { 17, 14, 36, 43 };//第一行4個元素
 4             interleavedArray[1] = new int[] { 77, 34 };     //第二行2個元素
 5             interleavedArray[2] = new int[3] { 55, 76, 89 };
 6             interleavedArray[3] = new int[6] { 33, 23, 56, 75, 46, 52 };
 7             //遍歷交錯數組
 8             //先測試下交錯數組1維長度(行)
 9             Console.WriteLine(interleavedArray.GetLength(0));   //第一種方法,獲取第一維長度
10             Console.WriteLine(interleavedArray.Length);      //推薦使用,獲取第一維長度.標准用法
11             Console.WriteLine(interleavedArray[0].Length); //(第一行有多少列)
12             Console.WriteLine(interleavedArray[1].Length); //第二行有多少列
13             //下面遍歷的問題就容易了
14             Console.ForegroundColor = ConsoleColor.Cyan;
15 
16             for (int i = 0; i < interleavedArray.Length; i++)   //行,一維
17             {
18                 for (int j = 0; j < interleavedArray[i].Length; j++)    //列,二維
19                 {
20                     Console.Write(interleavedArray[i][j].ToString().PadRight(4, ' '));
21                 }
22                 Console.WriteLine();
23             }

交錯數組應用代碼如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM