C#多维数组值意外更改

时间:2014-07-11 13:18:38

标签: c# multidimensional-array

你好,我在这段代码中有一个奇怪的行为:

public class IGraphics
{
    public  int[,] screen;

    private int[,] world;
    private int[,] entitys;
    private int[,] buffer;

    private int screenW;
    private int screenH;
    public IGraphics(int screenW, int screenH) {

        this.screenH = screenH;
        this.screenW = screenW;
        screen = new int[screenW + 1, screenH];
        buffer = new int[screenW + 1, screenH];


    }
    public void loadWorld(int[,] world) {
        this.world = world;
    }
    public void clear() {
        screen = new int[screenW + 1, screenH];
        world = new int[screenW, screenH];
        for (int y = 0; y < world.GetLength(1); y++) {
            for (int x = 0; x < world.GetLength(0); x++) {
                world[x, y] = 0;

            }

        }

    }

    private void loadScreen() {


    }
    private void updateEntitys()
    {
        entitys = new int[screenW, screenH];
        List<GameObject> EntRow = Common.world.getEntitys();
        for (int i = 0; i < EntRow.Count(); i++)
        {
            entitys[EntRow[i].x, EntRow[i].y] = EntRow[i].Icon;
        }

    }
    public void draw() {

        updateEntitys();
        for (int y = 0; y < screen.GetLength(1); y++)
        {
            for (int x = 0; x < screen.GetLength(0) - 1; x++)
            {
                if (entitys[x, y] == 0)
                {
                    screen[x, y] = world[x, y];
                }
                else
                {
                    screen[x, y] = entitys[x, y];

                }
            }

            screen[screen.GetLength(0) - 1, y] = 123;

        }
        if (buffer.Cast<int>().SequenceEqual(screen.Cast<int>()))
        {
            return;
        }
        Console.Clear();
        buffer = screen;
        for (int y = 0; y < screen.GetLength(1); y++) {
            for (int x = 0; x < screen.GetLength(0); x++) {
                if (screen[x, y] == 123)
                {
                    Console.WriteLine();
                }
                else {
                    Console.Write(objectStore.getIcon(screen[x, y]));
                }
            }

        }

    }

}

问题来自于Draw()函数,其中我设置了屏幕[,]数组的值由于某种原因它也改变了缓冲区[,]数组的值,之后控件也尝试移动缓冲区[,]在一个单独的班级,但我有同样的问题。

有人作为解释吗?

3 个答案:

答案 0 :(得分:1)

如果查看名为draw的方法的主体,您会注意到这个分配:

buffer = screen;

这可能是你注意到的改变的原因。

答案 1 :(得分:1)

将引用变量分配给另一个变量时,将指针复制到该变量,而不是复制内容,因此最终得到的是指向同一个数组的两个变量。

尝试使用克隆或复制等方式复制数组。我认为它的screen.CopyTo

screen.CopyTo(buffer, 0);

答案 2 :(得分:0)

请参阅其他答案,了解您的问题的可能原因。但是,以下代码显示更改screen [] 更改缓冲区[]。您的这种努力将使您能够将调查重点放在其他地方。原则是首先简单。

    int counter = 1;
    public void draw()
    {

        for (int y = 0; y < screen.GetLength(1); y++)
        {
            for (int x = 0; x < screen.GetLength(0) - 1; x++)
            {
                screen[x, y] = counter++;
            }
            screen[screen.GetLength(0) - 1, y] = 123;
        }
        if (buffer.Cast<int>().SequenceEqual(screen.Cast<int>()))
        {
            MessageBox.Show("Help!");
            return;
        }
        // check again
        for (int y = 0; y < screen.GetLength(1); y++)
        {
            for (int x = 0; x < screen.GetLength(0) - 1; x++)
            {
                if (screen[x, y] == buffer[x, y])
                {
                    MessageBox.Show("Help two!");
                    return;
                }
            }
        }