程序点滴jcodeer.blog.chinaunix.net
jcodeer
全部博文(171)
2012年(2)
2011年(70)
2010年(9)
2009年(14)
2008年(76)
Bean_lee
GY123456
armlife
linky521
g_progra
猥琐才是
athzhang
CU博客助
meiyang_
heq76
gaokeke1
yangyefe
yanganni
tomcodin
qhy95020
allaxin
suntao32
13661379
分类: C/C++
2008-04-13 15:56:51
using System; public class BasicArray { //数组的创建及基本使用方法 public static void BasicOp() { int[] Number = new int[10]; for (int i = 0; i < Number.Length; ++i) { Number[i] = i; Console.WriteLine(Number[i]); } } //使用foreach遍历数组 public static void ForeachOp() { int[] Number = new int[10]; for (int i = 0; i < Number.Length; ++i) { Number[i] = i; } foreach (int i in Number) Console.WriteLine(i); } //使用变长数组 public static void ParamArray() { int [] Number = new int [10]; for (int i = 0; i < Number.Length; ++i) { Number[i] = i; } PassVarArray(Number); } //定义变长数组操作函数 public static void PassVarArray(params int[] intValues) { foreach (int i in intValues) Console.WriteLine(i); } //多维数组 public static void RectArrayOp() { //Length得到整个数组的长度 const int row = 10; const int col = 10; int[,] Table = new int[row, col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) Table[i,j] = i * row + j; } //使用foreach访问数组中的每个值 foreach (int i in Table) Console.WriteLine(i); } //创建Jagged数组 public static void JaggedArrayOp() { const int row = 10; const int col = 10; int [][] Table = new int[row][]; for (int i = 0; i < row; i++) { Table[i] = new int[i]; for (int j = 0; j < Table[i].Length; j++) Table[i][j] = i * row + j; } //使用foreach访问数组中的每个值 foreach (int[] ele in Table) foreach(int i in ele) Console.WriteLine(i*i); } //使用系统数组定义的方法 public static void SystemArrayOp() { const int MAX_LENGTH = 10; Random rd = new Random(); int[] Number = new int[MAX_LENGTH]; for (int i = 0; i < MAX_LENGTH; i++) { Number[i] = (rd.Next() % 10); } foreach (int i in Number) Console.Write("{0} ", i); Console.WriteLine(); Array.Sort(Number); foreach (int i in Number) Console.Write("{0} ", i); Console.WriteLine(); Array.Reverse(Number); foreach (int i in Number) Console.Write("{0} ", i); Console.WriteLine(); } } public class CallBasicArray { public static void test() { BasicArray.BasicOp(); BasicArray.ForeachOp(); BasicArray.ParamArray(); BasicArray.RectArrayOp(); BasicArray.JaggedArrayOp(); Console.Clear(); BasicArray.SystemArrayOp(); } }
上一篇:C#教程之九(接口用法)
下一篇:VBScript教程之一(Hello VBScriopt)
登录 注册