数组的定义及遍历方式
2013-11-25 22:21
281 查看
/* 数组的定义及遍历方式 */ namespace Frank { public class Test { public static void Main(string[] args) { #region 一维数组 int[] array0;//声明对象 array0 = new int[4];//定义 array0[0] = 1;//赋值 int[] array1 = new int[4];//声明并定义 array1[0] = 2;//赋值 int[] array2 = new int[1]{1};//声明定义初始化值 int[] array3 = new int[]{1};//声明定义初始化 int[] array4 = {1};//数组简写模式 Test2[] t2 = new Test2[] { new Test2{Age = 20}, new Test2{Age = 30} }; //遍历一维数组 for(int p=0;p<t2.Length;p++) { System.Console.WriteLine(t2[p].Age); } #endregion #region 多维数组 int[,] i = new int[2,1];//二维 i[0,0] = 1; i[1,0] = 2; //遍历二维数组 for(int j = 0; j < i.GetLength(0) ;j++) { for(int k = 0;k < i.GetLength(1); k++) { System.Console.Write(i[j,k]+" "); } } System.Console.WriteLine(""); int[,,] i2 = new int[2,1,1];//三维 i2[0,0,0] = 0; i2[1,0,0] = 1; int[,,] i3 = {{{1,2},{3,4}},{{5,6},{7,8}}};//简写模式三维数组 //遍历三维数组 for(int t = 0; t < i3.GetLength(0); t++) { for(int y = 0; y < i3.GetLength(1);y++) { for(int u =0; u<i3.GetLength(2); u++) { System.Console.Write(i3[t,y,u]+" "); } } } System.Console.WriteLine(""); #endregion #region 锯齿数组 int[][] i4 = new int[3][]; i4[0] = new int[]{1,2,3,4}; i4[1] = new int[]{1,2,3}; i4[2] = new int[]{1}; //遍历锯齿数组 for(int p = 0; p < i4.Length; p++) { for(int u =0; u< i4[p].Length; u++) { System.Console.WriteLine(i4[p][u]); } } #endregion } } public class Test2 { public int Age{get;set;} } }
相关文章推荐
- (32)数组中常用的多遍历方式(33)PHP中预定义的超全局数组
- 第八讲 数组的定义与四种赋值方式、遍历和内存分析
- 迭代器循环数组和遍历Map的三种方式
- php定义数组的两种方式
- 二叉树系列——二叉树的定义以及各种遍历方式
- 黑马程序员——数组定义方式
- 黑马程序员—C学习笔记—结构体变量五种定义方式与指针结构体数组探究
- 遍历数组的所有元素的三种方式
- JS中遍历数组、对象的方式
- TypeScript 数组定义方式
- 一维数组的定义方式
- Java 数组的不同定义方式和数组长度为可变参数
- laravel查询得到的数据(为对象object)无法进行数组循环遍历,而且不能用数组方式访问和查看的原因以及解决方法
- android 数组的两种定义方式
- 【JavaScript】(3)基础语法---数组的定义、使用方式及其注意事项
- 遍历数组的四种方式
- 最好的遍历数组的指针方式
- 遍历数组的三种方式
- PHP数组定义与遍历
- 数组的定义和数组的遍历