学习C#数组(1)
来源:互联网 发布:淘宝上的好店铺推荐 编辑:程序博客网 时间:2024/05/16 10:03
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ARRAY{ class Program { static void Main(string[] args) { //初始化 char[] vowels = new char[5]; vowels[0] = 'v'; vowels[1] = 'o'; char[] vowels2 = new char[] { 'v', '0', 'w'}; //矩阵,类似于数组,但与数组类型不同 int[,] aInt = new int[3,3]; for(int i = 0; i < aInt.GetLength(0); i++) for(int j = 0; j < aInt.GetLength(1); j++) aInt[i,j] = i * 3 + j; for(int i = 0; i < aInt.GetLength(0); i++) { for(int j = 0; j < aInt.GetLength(1); j++) System.Console.Write(aInt[i,j]); System.Console.Write("\n"); } //锯齿数组 int[][] zigInt = new int[3][];//必须指定最外层的维数,里层的维数不可指定 for(int i = 0; i < zigInt.Length; i++) { zigInt[i] = new int[i]; for(int j = 0; j < zigInt[i].Length; j ++) zigInt[i][j] = i * j; } for (int i = 0; i < zigInt.Length; i++) { for (int j = 0; j < zigInt[i].Length; j++) System.Console.Write(zigInt[i][j]); System.Console.Write("\n"); } //多为数组初始化 int[][] matrix = new int[][]{ new int[]{0}, new int[]{0,1,2}, new int[]{0,1,2,3} }; for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[i].Length; j++) System.Console.Write(matrix[i][j]); System.Console.Write("\n"); } //边界检测 int[] arr = new int[3]; arr[3] = 1;//IndexOutOfRangeException } }}