C# 静态与动态数组

您所在的位置:网站首页 静态一维数组与动态一维数组 C# 静态与动态数组

C# 静态与动态数组

2024-07-13 06:37:17| 来源: 网络整理| 查看: 265

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


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭