新手求助。请教如何解读这段程序。谢谢。
using System;
using System.Collections.Generic;
using System.Text;namespace Demo_4._2
{
class Array2D
{
int[,] a;
int rows, cols;
public Array2D(int r, int c) //构造函数
{
rows = r;
cols = c;
a = new int[rows, cols];
} public int this[int index1, int index2] //二参数索引器
{
get { return a[index1, index2]; } //返回二维数组的一个元素
set { a[index1, index2] = value; } //对二维数组的一个元素赋值
}
}
public class Test
{
static void Main()
{
Array2D arr = new Array2D(2,3);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i,j]=i+j; //设置索引器
Console.Write(arr[i,j]+""); //显示索引器
}
Console.WriteLine();
}
}
}
}——————————————————————————---
关于arr[i,j]=i+j;
这里它是如何跟索引器关联起来的呢?
经过循环之后,应该是得出这些值吧
arr[0,0]=0
arr[0,1]=1
arr[0,2]=2
arr[1,0]=1
arr[1,1]=2
arr[1,2]=3i,j这里把值传入index1,index2
得到set{a[index1,index2]=value}
这里value是外部值,也就是得到
a[0,0]=0
a[0,1]=1
a[0,2]=2
a[1,0]=1
a[1,1]=2
a[1,2]=3
接着利用get{return a[index1,index2];}返回Array2D arr=new Array2D(2,3);
传入参数给数组 a=new int[2,3]
这后又要如何联系起来呢?是不是哪个地方理解错了。。
using System;
using System.Collections.Generic;
using System.Text;namespace Demo_4._2
{
class Array2D
{
int[,] a;
int rows, cols;
public Array2D(int r, int c) //构造函数
{
rows = r;
cols = c;
a = new int[rows, cols];
} public int this[int index1, int index2] //二参数索引器
{
get { return a[index1, index2]; } //返回二维数组的一个元素
set { a[index1, index2] = value; } //对二维数组的一个元素赋值
}
}
public class Test
{
static void Main()
{
Array2D arr = new Array2D(2,3);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i,j]=i+j; //设置索引器
Console.Write(arr[i,j]+""); //显示索引器
}
Console.WriteLine();
}
}
}
}——————————————————————————---
关于arr[i,j]=i+j;
这里它是如何跟索引器关联起来的呢?
经过循环之后,应该是得出这些值吧
arr[0,0]=0
arr[0,1]=1
arr[0,2]=2
arr[1,0]=1
arr[1,1]=2
arr[1,2]=3i,j这里把值传入index1,index2
得到set{a[index1,index2]=value}
这里value是外部值,也就是得到
a[0,0]=0
a[0,1]=1
a[0,2]=2
a[1,0]=1
a[1,1]=2
a[1,2]=3
接着利用get{return a[index1,index2];}返回Array2D arr=new Array2D(2,3);
传入参数给数组 a=new int[2,3]
这后又要如何联系起来呢?是不是哪个地方理解错了。。
现在还对赋值还是有点不明白。arr[]和a[]中间穿插了索引器感觉有点乱。
不过就先放着吧。。可能以后回过头来再看一下就懂了。。