C# 当我更改多维数组的值时,另一个数组相同位置的值也会更改。为什么?
我花了很多时间才弄明白这一点 我有两个多维数组:C# 当我更改多维数组的值时,另一个数组相同位置的值也会更改。为什么?,c#,arrays,multidimensional-array,conways-game-of-life,C#,Arrays,Multidimensional Array,Conways Game Of Life,我花了很多时间才弄明白这一点 我有两个多维数组: int[,] OldGeneration = new int[WidthX + 1, HeightY + 1]; int[,] NextGeneration = new int[WidthX + 1, HeightY + 1]; 稍后在我的代码中,在“NextGeneration”中设置了一些值之后,我使数组包含相同的值 OldGeneration = NextGeneration; 当我在运行程序时检查这些值时,它就正常工作了 稍低一点,然
int[,] OldGeneration = new int[WidthX + 1, HeightY + 1];
int[,] NextGeneration = new int[WidthX + 1, HeightY + 1];
稍后在我的代码中,在“NextGeneration”中设置了一些值之后,我使数组包含相同的值
OldGeneration = NextGeneration;
当我在运行程序时检查这些值时,它就正常工作了
稍低一点,然后我更改“NextGeneration”的一个值,当我这样做时,“OldGeneration”中的相同值也将更改
你能告诉我为什么吗
using System;
namespace CGoL
{
class CGoL_Base
{
static void Main(string[] args)
{
//Declare and define (initialize) Variables used by the Program
int WidthX = 5, HeightY = 5, Iterations = 5, Speed = 1000, Random = 0, CellsAlive = 0;
//Declare Multidimensional Arrays for actual generation and next generation
int[,] OldGeneration = new int[WidthX + 1, HeightY + 1];
int[,] NextGeneration = new int[WidthX + 1, HeightY + 1];
//########### Initialize "Game Board" ##########################
//Set Game Board to contain only dead cells
for (int y = 1; y <= HeightY; y++)
{
for (int x = 1; x <= WidthX; x++)
{
NextGeneration[x, y] = 0;
}
}
//Set pattern for oscillating (moving) structure with 3 living cells (Can be changed at will)
NextGeneration[3, 2] = 1;
NextGeneration[3, 3] = 1;
NextGeneration[3, 4] = 1;
//Set OldGeneration equal with NextGeneration so that the calculation can work
OldGeneration = NextGeneration;
//##################################################################
//Start the iterationcounter
for (int Iteration = 1; Iteration <= Iterations; Iteration++)
{
//########### Calculate actual generation ######################
//Calculate how the Game Board will change with the usual CGoL rules
if (Iteration >= 2) //Without this, the initialization above will not work
{
for (int y = 1; y <= HeightY; y++)
{
for (int x = 1; x <= WidthX; x++)
{
//########### Check surrounding Cells ##########################
//Check how much cells, surrounding the actual cell, are still alive
//to calculate later how the Game Board will change
CellsAlive = 0;
for (int i = -1; i <= 1; i++)
{
for (int n = -1; n <= 1; n++)
{
if (i == 0 && n == 0)
{
continue;
}
//Check if some Array Index will be out of Array Range (for example when index is smaller than 0)
if (x + n == 0 || x + n > WidthX || y + i == 0 || y + i > HeightY)
{
continue;
}
if (OldGeneration[(x + n), (y + i)] == 1)
{
CellsAlive++;
}
}
}
//##################################################################
//If a dead cell got 3 living neighbours, the cell will become alive in the next generation
if (OldGeneration[x, y] == 0 && CellsAlive == 3)
{
NextGeneration[x, y] = 1;
}
//If a living cell got less than 2 living neighbours, the cell will die in the next generation
else if (OldGeneration[x, y] == 1 && CellsAlive < 2)
{
NextGeneration[x, y] = 0; //OldGeneration[x, y] will be changed to 0 even if it is not written here????? why?
}
//If a living cell got 2 or 3 living neighbours, the cell will stay alive in the next generation
else if (OldGeneration[x, y] == 1 && (CellsAlive == 2 || CellsAlive == 3))
{
NextGeneration[x, y] = 1;
}
//If a living cell got more than 3 living neighbours, the cell will die in the next generation
else if (OldGeneration[x, y] == 1 && CellsAlive > 3)
{
NextGeneration[x, y] = 0;
}
}
}
Console.ReadKey();
}
//##################################################################
//########### Draw the "Game Board" ############################
//Makes the console window Empty :)
Console.Clear();
//"for" is making new rows with Console.Write("\n");
for (int y = 1; y <= HeightY; y++)
{
//"for" is writing "O"'s in one row
for (int x = 1; x <= WidthX; x++)
{
if (NextGeneration[x, y] == 1)
{
Console.Write("O ");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
Console.WriteLine("Iteration: {0}", Iteration);
System.Threading.Thread.Sleep(Speed);
//At the end, make the actual generation same like the old generation to be calculated next
OldGeneration = NextGeneration;
//##################################################################
}
Console.ReadKey();
}
}
}
使用系统;
命名空间CGoL
{
类CGoL_基
{
静态void Main(字符串[]参数)
{
//声明并定义(初始化)程序使用的变量
int WidthX=5,HeightY=5,Iterations=5,Speed=1000,Random=0,CellsAlive=0;
//为实际生成和下一代声明多维数组
整数[,]OldGeneration=新整数[WidthX+1,HeightY+1];
int[,]NextGeneration=newint[WidthX+1,HeightY+1];
//###########初始化“游戏板”##########################
//将游戏板设置为仅包含死区
对于(int y=1;y当您声明:
OldGeneration = NextGeneration;
您正在将NextGeneration的引用指定给OldGeneration,因此您对其中一个所做的任何更改都会反映到另一个,这是正常的
如果要创建不相交的副本,应该使用数组。copy
()这一行就是问题所在:OldGeneration=NextGeneration;
在这一行之前有两个对象(两个数组),在这一行之后有一个对象,两个变量都可以查看。您必须在双循环中复制内容,而不是上面的那一行。这是因为您没有使数组包含相同的值,所以您将一个数组的引用替换为对另一个数组的引用 从那时起,两个变量引用同一个数组。当您使用一个变量更改数组时,当您使用另一个变量查看数组时,该更改将可见,因为它是同一个数组 如果要将数据从一个阵列复制到另一个阵列,可以使用: 使用此行:
OldGeneration = NextGeneration;
使OldGeneration
变量引用与NextGeneration
变量相同的内存
您可以改为复制阵列:
Array.Copy(NextGeneration, OldGeneration, NextGeneration.Length);
非常感谢。这对我来说很有效。通常我在Perl中使用哈希,在那里它可以使它们“相同”。谢谢。这不起作用。它告诉我它只适用于一维数组。
Array.Copy(NextGeneration, OldGeneration, NextGeneration.Length);