简单描述C#二维数组

开发 后端
本文介绍C#二维数组,在C#中int[][] myInt是声明一个交错数组,声明C#二维数组是这么声明int[,] myInt。

C#有很多值得学习的地方,这里我们主要介绍C#二维数组,包括介绍声明C#二维数组是这么声明int[,] myInt等方面。

C#二维数组

  1. publicclassArray2D...{  
  2. publicstaticvoidmain(String[]args)...{  
  3. intmyInt[][]=newint[5][10];  
  4. //遍历,给数组中的每一个数组赋值  
  5. for(inti=0;i<myInt.length;i++)...{  
  6. for(intj=0;j<myInt[0].length;j++)...{  
  7. myInt[i][j]=i*j;  
  8. }  
  9. }  
  10. System.out.println("myInt.length="+myInt.length+",myInt[0].
    length
    ="+myInt[0].length);  
  11. //输出数组每一维的下限和上限  
  12. for(inti=0;i<myInt.length;i++)...{  
  13. for(intj=0;j<myInt[0].length;j++)...{  
  14. System.out.println("myInt["+i+"]["+j+"]="+myInt[i][j]);  
  15. }  
  16. }  
  17. }  

在C#中int[][] myInt是声明一个交错数组,声明C#二维数组是这么声明int[,] myInt,上面的代码如果换成C#的,需要如下表示:

  1. classclsArrat2D  
  2. {  
  3. /**////<summary> 
  4. ///应用程序的主入口点。  
  5. ///summary> 
  6. [STAThread]  
  7. staticvoidMain(string[]args)  
  8. {  
  9. int[,]myInt=newint[5,10];  
  10. //遍历,给数组中的每一个数组赋值  
  11. for(inti=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)  
  12. {  
  13. for(intj=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)  
  14. {  
  15. myInt[i,j]=i*j;  
  16. }  
  17. }  
  18. //输出数组每一维的下限和上限  
  19. for(inti=0;i<myInt.Rank;i++)  
  20. {  
  21. Console.WriteLine("{0}{1}{2}",i,myInt.GetLowerBound(i),myInt.GetUpperBound(i));  
  22. }  
  23. //遍历,输出二维数组中每一个元素的个数  
  24. for(inti=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)  
  25. {  
  26. for(intj=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)  
  27. {  
  28. Console.WriteLine("myInt[{0},{1}]={2}",i,j,myInt[i,j]);  
  29. }  
  30. }  
  31. Console.ReadLine();  
  32. }  

以上介绍C#二维数组。

【编辑推荐】

  1. C#记忆功能的地址栏控件
  2. 描述C#调用外部进程
  3. C#语言操纵数据库事务
  4. 概述C#语言异常处理
  5. 简单介绍C#数组和函数
责任编辑:佚名 来源: 51cto.com
相关推荐

2009-09-16 09:01:40

C#多维数组

2009-09-18 11:33:37

C#二维数组初始化

2009-09-02 10:23:52

C#动态二维数组

2009-09-17 16:28:27

C#参差数组

2009-08-31 18:32:01

C# ListBoxE

2009-08-19 10:09:21

C#和C++

2009-09-07 15:31:49

C#支持事件

2009-08-27 10:01:52

C#自动属性

2009-09-01 17:08:35

C# Color枚举

2009-09-03 16:55:58

C#引用类型

2009-08-20 16:45:03

C#哈希值

2009-08-21 17:31:58

C#垃圾回收

2009-09-02 13:01:11

C#多路广播

2009-08-28 09:30:48

C#命名属性

2009-09-01 18:25:32

C#结构实例

2009-09-07 14:33:02

C# switch语句

2009-08-13 17:58:34

C#存储过程

2009-08-20 11:38:15

C#二维数组

2009-08-18 14:46:16

C# 操作Excel

2009-09-16 17:29:10

Linq查询二维数组
点赞
收藏

51CTO技术栈公众号