C# 静态与动态数组 |
您所在的位置:网站首页 › 静态一维数组与动态一维数组 › C# 静态与动态数组 |
C#中的数组是由System.Array类衍生出来的引用对象,因此可以使用Array类中的各种方法对数组进行各种操作。 一维数组: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义一维数组 int[] IntArray = new int[10] {1,2,3,4,5,6,7,8,9,10}; // 定义一维字符串数组 string[] StrArray = new string[3]; StrArray[0] = "abc" ; StrArray[1] = "abc"; Console.ReadKey(); } } }删除元素(一维数组): using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义一维数组 int[] IntArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 遍历数组 foreach (int num in IntArray) Console.WriteLine(num); Console.ReadLine(); // 通过循环删除第三个元素 int Del_Num = 2; for (int x = Del_Num; x < IntArray.Length - Del_Num; x++) { IntArray[x] = IntArray[x - 1]; } Console.ReadKey(); } } }寻找最大最小值: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义一维数组 int[] Array = new int[10] { 57, 32, 78, 96, 33, 11, 78, 3, 78, 2 }; // 声明两个变量用来存储最大值和最小值 int min = int.MaxValue; int max = int.MinValue; int sum = 0; for (int i = 0; i < Array.Length; i++) { if (Array[i] > max) max = Array[i]; if (Array[i] < min) min = Array[i]; sum += Array[i]; } Console.WriteLine("最大值: {0} 最小值: {1} 总和: {2} 平均值: {3}", max, min, sum, sum / Array.Length); Console.ReadKey(); } } }数组组合为字符串: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" }; string str = null; for (int x = 0; x < name.Length - 1; x++) str += name[x] + "|"; Console.WriteLine(str + name[name.Length - 1]); Console.ReadKey(); } } }数组元素反转: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" }; string tmp; for (int x = 0; x < name.Length / 2; x++) { tmp = name[name.Length - 1 - x]; name[x] = name[name.Length - 1 - x]; name[name.Length - 1 - x] = tmp; } for (int x = 0; x < name.Length - 1; x++) Console.Write(name[x] + " |" ); Console.ReadKey(); } } }冒泡排序: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { // 执行排序 static void Sort(int[] Array) { for (int x = 0; x < Array.Length - 1; x++) { for (int y = 0; y < Array.Length - 1 - x; y++) { if (Array[y] > Array[y + 1]) { int tmp = Array[y]; Array[y] = Array[y + 1]; Array[y+1] = tmp; } } } } // 输出结果 static void Display(int[] Array) { for (int x = 0; x < Array.Length; x++) { Console.Write(Array[x] + " "); } } static void Main(string[] args) { int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 }; Sort(MyArray); Display(MyArray); // 使用系统提供的方法排序 Array.Sort(MyArray); // 执行一次反向排序 Array.Reverse(MyArray); Console.ReadKey(); } } }直接插入排序: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { // 执行排序 static void Sort(int[] Array) { for (int x = 0; x < Array.Length; x++) { int tmp = Array[x]; int y = x; while ((y > 0) && (Array[y - 1] > tmp)) { Array[y] = Array[y-1]; --y; } Array[y] = tmp; } } static void Main(string[] args) { int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 }; Sort(MyArray); foreach (int x in MyArray) Console.Write(x + " "); Console.ReadKey(); } } }选择排序: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { // 执行排序 static void Sort(int[] Array) { int min = 0; for (int x = 0; x < Array.Length; x++) { min = x; for (int y = x + 1; y < Array.Length; y++) { if (Array[y] < Array[min]) min = y; } int tmp = Array[min]; Array[min] = Array[x]; Array[x] = tmp; } } static void Main(string[] args) { int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 }; Sort(MyArray); foreach (int x in MyArray) Console.Write(x + " "); Console.ReadKey(); } } }定义二维数组 using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义二维数组 int[,] Array = new int[2,3]{{1,2,4},{4,5,6}}; Console.WriteLine("数组行数为: {0}", Array.Rank); Console.WriteLine("数组列数为: {0}", Array.GetUpperBound(Array.Rank - 1) + 1); for (int x = 0; x < Array.Rank;x++ ) { string str = ""; for(int y=0;y< Array.GetUpperBound(Array.Rank-1)+1;y++) { str = str + Convert.ToString(Array[x, y]) + " "; } Console.Write(str); } Console.ReadKey(); } } }定义动态二维数组: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int Row = Convert.ToInt32(Console.ReadLine()); int Col = Convert.ToInt32(Console.ReadLine()); int[,] Array = new int[Row, Col]; for (int x = 0; x < Row; x++) { for (int y = 0; y < Col; y++) { Console.Write(x + "-->" + y.ToString() + " "); } Console.WriteLine(); } Console.ReadKey(); } } }一维数组的合并: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] Array1 = new int[] { 1, 2, 3, 4, 5 }; int[] Array2 = new int[] { 6, 7, 8, 9, 10 }; // 将Array1 与 Array2 合并成 Array3 int Count = Array1.Length + Array2.Length; int[] Array3 = new int[Count]; for (int x = 0; x < Array3.Length; x++) { if (x < Array1.Length) Array3[x] = Array1[x]; else Array3[x] = Array2[x - Array1.Length]; } foreach (int each in Array3) Console.Write(each + " "); Console.ReadKey(); } } }二维数组的合并: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] Array1 = new int[] { 1, 2, 3, 4, 5 }; int[] Array2 = new int[] { 6, 7, 8, 9, 10 }; // 将两个一维数组,合并到一个二维数组中 int[,] Array3 = new int[2, 5]; // Rank = 二维数组中的2 for (int x = 0; x < Array3.Rank; x++) { switch (x) { case 0: { for (int y = 0; y < Array1.Length; y++) Array3[x, y] = Array1[y]; break; } case 1: { for (int z = 0; z < Array2.Length; z++) Array3[x, z] = Array2[z]; break; } } } // 输出二维数组中的数据 for (int x = 0; x < Array3.Rank;x++ ) { for(int y=0;y |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |