Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的二维数组在改变?_C#_Arrays - Fatal编程技术网

C# 为什么我的二维数组在改变?

C# 为什么我的二维数组在改变?,c#,arrays,C#,Arrays,我有以下代码: static void Main(string[] args) { int m = 4, n = 5; int[,] a = new int[m, n]; for (int i = 0; i < a.GetLength(0); i++) for (int j = 0; j < a.GetLength(1); j++) a[i, j] = rand

我有以下代码:

    static void Main(string[] args)
    {
        int m = 4, n = 5; 
        int[,] a = new int[m, n];

        for (int i = 0; i < a.GetLength(0); i++)
            for (int j = 0; j < a.GetLength(1); j++)
                a[i, j] = random.Next(10);


        VypisMatici(a);


        int[,] a0 = a;
        Console.WriteLine("Chessboard: ");
        for (int i = 0; i < a0.GetLength(0); i++)
        {
            for (int j = 0; j < a0.GetLength(1); j++)
            {
                if (((j % 2 == 0) && (i % 2 == 0) && (a0[i, j] % 2 == 0)) || ((j % 2 != 0) && (i % 2 != 0) && (a0[i, j] % 2 == 0)) || ((j % 2 != 0) && (i % 2 == 0) && (a0[i, j] % 2 != 0)) || ((j % 2 == 0) && (i % 2 != 0) && (a0[i, j] % 2 != 0)))
                {
                    a0[i, j]++;
                }
            }
        }



        Console.WriteLine("---------");
        for (int i = 0; i < a0.GetLength(0); i++)
        {
            for (int j = 0; j < a0.GetLength(1); j++)
                Console.Write("{0,2}, ", a0[i, j]);
            Console.WriteLine();
        }

        Console.WriteLine("Vypsání v opačném pořadí: ");
        int[,] a1 = new int[a.GetLength(0), a.GetLength(1)];
        for (int i = 0; i < a1.GetLength(0); i++)
        {
            for (int j = 0; j < a1.GetLength(1); j++)
            {
                a1[i, a1.GetLength(1) - (j + 1)] = a[i, j];
            }

        }
        VypisMatici(a1);
        Console.WriteLine("Prohození prvků na řádcích: ");
        int temp;
        int[,] a2 = a;
        for (int i = 0; i < a2.GetLength(0); i +=2)
        {
            for (int j = 0; j < a2.GetLength(1); j++)
            {
                if (a2.GetLength(0) - 1 > i)    
                {
                    temp = a2[i, j];
                    a2[i, j] = a2[i + 1, j];
                    a2[i + 1, j] = temp;
                }
            }
        }
        VypisMatici(a2);
        Console.ReadLine();
    }

    static void VypisMatici(int[,] matice)
    {
        // Vypsání matice
        for (int i = 0; i < matice.GetLength(0); i++)
        {
            for (int j = 0; j < matice.GetLength(1); j++)
                Console.Write("{0,2}, ", matice[i, j]);
            Console.WriteLine();
        }
        Console.WriteLine();
    }
}
static void Main(字符串[]args)
{
int m=4,n=5;
int[,]a=新的int[m,n];
for(int i=0;ii)
{
温度=a2[i,j];
a2[i,j]=a2[i+1,j];
a2[i+1,j]=温度;
}
}
}
Vypismatic(a2);
Console.ReadLine();
}
静态孔隙Vypismatic(内部[,]材质)
{
//Vypsánímatice
for(int i=0;i

重要的代码是“棋盘”及以上。棋盘应该增加数组的一些元素,这样我就得到了数组,它看起来像一个棋盘-奇数将是白色的正方形,偶数将是黑色的正方形。我的问题是,数组a在棋盘循环之后,与a0相同。我想知道为什么。(很抱歉使用捷克语的字符串-实际上捷克语部分在这个问题上并不重要)。很抱歉我的英语不好。谢谢帮助。

int[,]a0=a;
行中,您没有复制数组,而是给它起了第二个名称(参考)在
int[,]a0=a;
行中,您没有复制数组,而是为内存中的同一对象赋予了第二个名称(引用)。因此,当您修改a0时,a也会更改