C#数组 一维数组、二维数组、三维数组
2014-01-10 12:54
281 查看
一位数组:
初始化:int[] arr = new int[5] = {1,2,3,4,5};using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wm110_5 { class Program { static void Main(string[] args) { int[] arr = new int[5]; //定义一个具有5个元素的数组 Console.WriteLine("请输入一数组:"); for (int i = 0; i < 5; ++i) { arr[i] = Convert.ToInt32(Console.ReadLine()); //往数组中输入值 } for (int i = 0; i < 5; ++i) { int temp = arr[i]; int j = i; while ((j > 0) && (arr[j - 1] > temp)) { arr[j] = arr[j - 1]; --j; } arr[j] = temp; } Console.WriteLine("排序后:"); foreach (int n in arr) { Console.WriteLine("{0}",n); } } } }运行结果:
请输入一数组:
12
12
34
11
53
排序后:
11
12
12
34
53
请按任意键继续. . .
二维数组:
声明一个二维数组: int[,] arr2 = new int[3,2];初始化:int[,] arr2 = new int[3,2]{{1,2},{3,4},{5,6}};
示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wm110_6 { class Program { static void Main(string[] args) { Console.Write("请输入数组的行数:"); int row = Convert.ToInt32(Console.ReadLine()); //获取输入的行数 Console.Write("请输入数组的列数:"); int col = Convert.ToInt32(Console.ReadLine()); //获取输入的行数 int[,] arr2 = new int[row, col]; //根据行列实例化一个数组 Console.WriteLine("结果:"); for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { if (i == j) { Console.Write("#"); } else { Console.Write("@"); } } Console.WriteLine(); //换行 } } } }运行结果:
请输入数组的行数:13
请输入数组的列数:14
结果:
#@@@@@@@@@@@@@
@#@@@@@@@@@@@@
@@#@@@@@@@@@@@
@@@#@@@@@@@@@@
@@@@#@@@@@@@@@
@@@@@#@@@@@@@@
@@@@@@#@@@@@@@
@@@@@@@#@@@@@@
@@@@@@@@#@@@@@
@@@@@@@@@#@@@@
@@@@@@@@@@#@@@
@@@@@@@@@@@#@@
@@@@@@@@@@@@#@
请按任意键继续. . .
三维数组:
初始化:int[,,]arr3 = new[2,1,3] = {{{1,2,3}},{{4,5,6}}};示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wm110_7 { class Program { static void Main(string[] args) { int[, ,] arr3; //声明一个三维数组 arr3 = new int[,,] { {{ 1, 2, 3 }}, {{ 4, 5, 6 }} }; //初始化三维数组,维数为[2,1,3] foreach (int i in arr3) { Console.WriteLine(i); } } } }运行结果:
1
2
3
4
5
6
请按任意键继续. . .
相关文章推荐
- 一维数组,二维数组,三维数组名的含义
- C++中动态定义一维数组,二维数组,三维数组的方法
- C语言 通过指针访问一维数组,二维数组,三维数组。
- C++中动态定义一维数组,二维数组,三维数组
- 一维数组,二维数组,三维数组,多维数组整理
- 简洁明了,C++中动态定义一维数组,二维数组,三维数组
- 一维数组,二维数组,三维数组,数组与指针,结构体数组,通过改变指针类型改变访问数组的方式
- 简洁明了,C++中动态定义一维数组,二维数组,三维数组
- 浅谈java中的一维数组、二维数组、三维数组、多维数组
- 【转】C++中new动态定义一维数组,二维数组,三维数组
- C++中动态定义一维数组,二维数组,三维数组
- new创建 一维数组、二维数组、三维数组、四维数组...
- 动态申请一维、二维、三维数组方法
- 《C++笔记》 Part8 一维、二维、三维数组的动态申请及用vector的表示方法
- java学习初探八之数组一维数组、二维数组
- PHP二维数组(或任意维数组)转换成一维数组的方法汇总
- 一维数组题目训练,二维数组巩固
- 一维数组、二维数组、字符数组、指针 相关的 sizeof 计算
- 一维&&二维&&三维数组的建立
- foreach便利二维数组,键值对,以值为键变为三维数组