获取设置三维数组 [英] Get set three dimensional array

查看:73
本文介绍了获取设置三维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是什么而不是价值?





What should be instead of "value"?


bool[,,] A2;

bool[,,] A
{
  get { return A2[value,value,value]; }

  set  
    {
        if(A2[value,value,value]!=value] 
       {
         progressMade=true;
         A2[value,value,value]=value;
       }
    }
}

推荐答案

我认为这是你想要的;如果不是,我很想知道你:
I think this is kind of what you want; if it's not, I'd be curious to hear from you:
public class IndexedArray
{
    public bool[,,] A2 { set; get; }

    public bool progressMade { set; get; }

    public List<int> lastChangedIndexes { set; get; }

    public IndexedArray(int d1, int d2, int d3)
    {
        progressMade = false;
        A2 = new bool[d1,d2,d3];
        lastChangedIndices = new List<int>();
    }

    // the indexer
    public bool this[int x, int y, int z]
    {
        get  { return A2[x, y, z]; }

        set
        {
            progressMade = A2[x, y, z] != value;

            if (progressMade)
            {
                lastChangedIndices .Add(x);
                lastChangedIndices .Add(y);
                lastChangedIndices .Add(z);

                A2[x, y, z] = value;
            }
        }
    }
}

测试:

IndexedArray testArrayProp = new IndexedArray(3, 4, 5);

testArrayProp[1, 2, 3] = true;

bool check = testArrayProp[1, 2, 3];

var changedIndices = testArrayProp.LastChangedIndices;

我不会使用这种类型的解决方案来解决你的问题管理一个数组,并跟踪变化...如果这就是你在这之后的真实情况。



当然,你不能继承'数组,或者使用'数组作为通用类中的约束,因为数组是密封的。根据具体情况,我会使用列表。



在您想要动态通知变更时,您可以实现INotifyPropetyChanged,而不是使用非动态跟踪此处使用的变量。

I would not use this type of solution to the problem of how you manage an Array, and keep track of change ... if that's what you are really after here.

Of course, you can't inherit from 'Array, or use 'Array as a constraint in a Generic Class, because Array is sealed. Depending on the circumstances I would go for using Lists.

At the point you wanted to dynamic notifications of changes, you could implement INotifyPropetyChanged, rather than use the non-dynamic tracking variables used here.


class AX
        {
            public bool this[int x, int y, int i]
            {
                get
                {
                    return A2[x, y, i];
                }

                set
                {
                    if (A2[x, y, i])
                        progressMade = true;

                    A2[x, y, i] = value;
                }
            }
        }

AX A = new AX();

static bool[, ,] A2 = new bool[9, 9, 9];


这篇关于获取设置三维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆