我有这样的课程:
class Data
{
public int[,] G;
public int[,] R;
public Data(int[,] G, int[,] R)
{
this.G = G;
this.R = R;
}
}
和这样的课程:
class Calculation
{
public Data data;
public Form1 f1 { get; set; }
public int[,] G;
public int[,] R;
public int[,] R_def;
public Calculation(Form1 f1, Data data)
{
this.f1 = f1;
this.data = data;
this.G = data.G;
this.R = data.R;
this.n = G.GetLength(0);
this.R_def = data.R;
}
public bool ant_calc(int start_)
{
... some calculation, where in result my R element's get 0;
but nothing is done with R_def;
}
public void calculate(int start__)
{
int first_edge = start__;
for (int o = 0; o < 10; o++)
{
bool a_c = ant_calc(start__);
while (a_c != true)
{
a_c = ant_calc(start);
start__ = start;
}
f1.listBox1.Items.Add("------");
L_max = 0;
R = R_def;
passengers = 0;
where_been = new int[n, n];
curr_R = new int[n, n];
start__ = first_edge;
}
}
并组成部分:
private void loadData()
{
this.reader = new Reader("test1.txt");
Data data = new Data(reader.G, reader.R);
Calculation calc = new Calculation(this, data);
calc.get_shortest_path();
calc.construct_new_graph();
calc.calculate(0);
}
我的问题在于,如何在这样的界限:R = R_def;我可以设置我的第一个R值,我从表单发送?我没有用R_def在代码中进行任何编辑,但它的行为和R做的相同,为什么?我如何将R_def设置为冻结,我包含默认data.R值,并且永远不会更改它?
如果事情不明确,请写下评论......
答案 0 :(得分:1)
R = R_def
分配引用,而不是值。换句话说,R
和R_def
然后指向内存中的相同数组。您应该阅读一些初学者的C#书中的参考类型和值类型。
答案 1 :(得分:1)
这是您使用Array.Copy()
在两个2D数组之间复制值的方法:
static void Main(string[] args)
{
// Init and fill array
int[,] R=new int[10, 20];
for(int i=0; i<10; i++)
{
for(int j=0; j<20; j++)
{
R[i, j]=20*i+j;
}
}
// Init new array
int[,] G=new int[10, 20];
// Copy 2D array
Array.Copy(R, G, R.Length);
}