填充哈希表在 c# 中遍历它并在文本框中显示它

Filling hashtable itinerating through it in c# and showing it in aTextBox

我正在尝试填充哈希表,键只是一个数字 ID,内容是一个整数数组。我无法在 TextBox 中显示它,我认为问题出在填充和显示上。

填充码为:

        Hashtable map = new Hashtable();

        for (int cooX = 0, cont = 0 ; cooX < maxX; cooX++)
        {
            for (int cooY = 0; cooY < maxY; cooY++)
            {
                for (int cooZ = 0; cooZ < maxZ; cooZ++)
                {
                    coordinate[0] = 0 + cooX;
                    coordinate[1] = 0 + cooY;
                    coordinate[2] = 0 + cooZ;

                    map.Add(cont, coordinate);

                    cont++;
                }
            }
        }

显示部分为:

        foreach ( DictionaryEntry i in map )
        {
            TextBox txt = new TextBox();

            txt.AppendText(i.Key + " : " + i.Value);

            MainGrid.Children.Add(txt);
        }

文本框显示:

"0 : System.Int32[]"

谢谢大家

编辑:哇,这对我来说太愚蠢了,我已经更改了我的代码,现在只需在 for 之前声明 TextBox 并在 for 之后显示它,它就可以有效地显示 Hashtable。

新密码是:

        TextBox txt = new TextBox();

        foreach ( DictionaryEntry i in map )
        {
            txt.AppendText(i.Key + " : " + i.Value + "\n");
        }

        MainGrid.Children.Add(txt);

所以这肯定只是一个转换问题。现在要调查一下,很抱歉犯了一个大错误。

新输出为:

(...) 3:系统.Int32[] 2:系统.Int32[] 1 : 系统.Int32[] 0:System.Int32[]

下面的 msmolcic 提供的新循环代码是:

foreach ( DictionaryEntry i in map )
{
int[] coords = (int[])i.Value;

txt.AppendText(string.Format("{0} : [ x={1}, y={2}, z={3} ]\n", i.Key, coords[0], coords[1], coords[2]));
}

现在显示如下,还是坏了:

4 : [ x=4, y=4, z=4 ]
3 : [ x=4, y=4, z=4 ]
2 : [ x=4, y=4, z=4 ]
1 : [ x=4, y=4, z=4 ]
0 : [ x=4, y=4, z=4 ]

您对地图中的所有元素使用相同的坐标实例。所以在每次迭代中,你都会覆盖它的内容 您必须每次都创建一个新实例,它将解决您的问题

编辑

如果您想拥有更简洁的代码,我建议您使用更面向对象的方法并定义您自己的坐标 class。然后您可以覆盖 ToString 方法。如果可能的话,你还应该用字典替换你的哈希表,因为这个是键入的

var dict = new Dictionary<int, Coordinate>();
var count = 0;

for (var cooX = 0; cooX < 2; cooX++)
{
    for (var cooY = 0; cooY < 2; cooY++)
    {
        for (var cooZ = 0; cooZ < 2; cooZ++)
        {

            dict.Add(count++, new Coordinate { X = cooX, Y = cooY, Z = cooZ });
        }
    }
}

TextBox txt = new TextBox();
foreach (var i in dict)
{
    var coord = i.Value;
    txt.AppendText(string.Format("{0} : {1}\n", i.Key, coord));
}

MainGrid.Children.Add(txt);

坐标:

class Coordinate
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }

    public override string ToString()
    {
        return string.Format("[ x={0}, y={1}, z={2} ]", X, Y, Z);
    }
}

如果您已经在使用 VS2015,您还可以使用字符串插值将 string.Format 替换为更具可读性的语法:

    public override string ToString()
    {
        return $"[ x={X}, y={Y}, z={Z} ]";
    }

同一个线程中的 sroll 和 msmolcic 已经提供了正确的解决方案修复,它们中的每一个都是问题之一。因此,为了保持简洁,我将在此处 post 最终代码。如果有人为此提供了更好、更清晰的代码,我将 select 你的答案作为更好的解决方案:

        Hashtable map = new Hashtable();

        TextBox txt = new TextBox();

        int[] coordinate = new int[3];

        for (int cooX = 0, cont = 0; cooX < 2; cooX++)
        {
            for (int cooY = 0; cooY < 2; cooY++)
            {
                for (int cooZ = 0; cooZ < 2; cooZ++)
                {
                    coordinate[0] = 0 + cooX;
                    coordinate[1] = 0 + cooY;
                    coordinate[2] = 0 + cooZ;

                    int[] coo = new int[3] { coordinate[0], coordinate[1], coordinate[2] };

                    map.Add(cont, coo);

                    cont++;
                }
            }
        }

        foreach (DictionaryEntry i in map)
        {
            int[] coords = (int[])i.Value;

            txt.AppendText(string.Format("{0} : [ x={1}, y={2}, z={3} ]\n", i.Key, coords[0], coords[1], coords[2]));
        }

        MainGrid.Children.Add(txt);