首页 > 编程语言 > 详细

c# 数组

时间:2018-09-25 13:36:31      阅读:187      评论:0      收藏:0      [点我收藏+]

定义数组

1 int[] test1;  //定义一个新数组
2 test1 = new int[10];   //定义数组长度(此时所有元素都是0)
3 
4 int[] test2 = new int[] { 1, 2, 3, 4, 5, 6 };  //也可以直接定义元素
5 
6 int[,] test3 = new int[3, 4];  //定义多维数组
7 
8 int[][] test4 = new int[9][];  //定义交错数组
1 int[] intArry = Array.CreateInstance(typeof(int), 10) as int[]; //(类型,长度)

 

数组之间的拷贝

 

1 //(原数组,原数组开始拷贝索引,新数组,新数组开始拷贝索引,拷贝元素个数)
2 Array.Copy(intArry, 3, intArry2, 4, intArry.Length-4); //数组的拷贝

 

二维数组

 1 int[,] test2 = new int[9, 9];   //二维数组
 2 for (int i = 1;i <= test2.GetLength(0); i++)  //Getlength(维度)取高维数组中某维度的长度
 3 {
 4     for(int j =1;j<=i;j++)
 5     {
 6 
 7         test2[i - 1, j - 1] = i * j;
 8         Console.Write(test2[i - 1, j - 1]);//不换行打印
 9     }
10     Console.WriteLine();//换行打印
11 }        

交错数组

 

 1 int[][] test1 = new int[9][];   //交错数组
 2             
 3 for (int i =1;i<10;i++)
 4 {
 5     test1[i - 1] = new int[i];   //把每行的列数组定义为[0,0,0,0,0,0]这种
 6     for (int j = 1;j<=i;j++)
 7     {
 8         test1[i-1][j-1]= i * j;  //把每行的[0,0,0,0,0]这种列数组重新复制
 9 
10         Console.Write(test1[i - 1][j - 1]);   
11     }
12     Console.WriteLine();   
13 }

 

c# 数组

原文:https://www.cnblogs.com/xt112233/p/9698935.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!