java中使用二维 数组
public class Array2D...{
public static void main(String[] args)...{
int myInt[][]=new int[5][10];
//遍历,给数组中的每一个数组赋值
for(int i=0;i<myInt.length;i++)...{
for(int j=0;j<myInt[0].length;j++)...{
myInt[j]=i*j;
}
}
System.out.println ("myInt.length="+myInt.length+",myInt[0].length="+myInt[0].length);
//输出数组每一维的下限和上限
for(int i=0;i<myInt.length;i++)...{
for(int j=0;j<myInt[0].length;j++)...{
System.out.println ("myInt["+i+"]["+j+"]="+myInt[j]);
}
}
}
}
在C#中int[][] myInt是声明一个交错数组,声明二维数组是这么声明int[,] myInt,上面的代码如果换成C#的,需要如下表示:
class clsArrat2D
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
int[,] myInt=new int[5,10];
//遍历,给数组中的每一个数组赋值
for(int i=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
{
for(int j=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
{
myInt[i,j]=i*j;
}
}
//输出数组每一维的下限和上限
for(int i=0;i<myInt.Rank;i++)
{
Console.WriteLine("{0} {1} {2}", i, myInt.GetLowerBound(i), myInt.GetUpperBound(i));
}
//遍历,输出二维数组中每一个元素的个数
for(int i=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
{
for(int j=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
{
Console.WriteLine("myInt[{0},{1}]={2}",i,j,myInt[i,j]);
}
}
Console.ReadLine();
}
}
c#中的Array是个类,有两个方法,一个属性,很重要。
一个属性是Rank 获得维度数
两个方法是GetLowerBound,,,
GetUpperBound 传入的参数为具体某个维度,然后获得相应的上下边界。
本文详细介绍了如何在Java和C#中声明及使用二维数组。通过示例代码展示了数组初始化、遍历赋值及输出的过程,并特别指出C#中获取数组维度和界限的方法。
369

被折叠的 条评论
为什么被折叠?



