如何保存多维数组索引? [英] How to Save a Multidimensional Array Index?

查看:54
本文介绍了如何保存多维数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的第一个任务是将"0"的位置保存为整数.使用标准数组,真正简单.该代码循环遍历一个数组(大小:8),直到找到0,然后将其保存为位置.参见下面的代码:

Basically my first task was to save the position of the '0' in an integer. Real simple with a standard array. This code loops through an array (Size: 8) until it locates the 0, then save that as the position. See code below:

p.s:n是对保存在其他地方的数组的引用.

p.s: n is a reference to an array saved somewhere else.

int position = 0;
        this.nodesExpanded++;
        // Loop through the array to get the position of where '0' is
        for (int i = 0; i < n.getPuzzle().length; i++){
            if (n.getPuzzle()[i] == 0){
                position = i;
                break;
            }
        }

我的最终任务是为多维数组(大小:[3,3])实现这一目标.因此,这就是我到目前为止创建的内容:

My ultimate task was to make this possible for a multidimensional array (Size: [3, 3]). So here's what I've created thus far:

for (int x = 0; x < 3; x++)
        {
            for (int y = 0; y < 3; y++)
            {
                if (n.getPuzzle()[x,y] == 0)
                {
                    **position = ;**
                    break;
                }
            }//end y loop
        }//end x loop

那么我该如何将对某个位置的数组引用保存为一个值?位置"必须是我猜不到的int.

So how do I go about saving an array reference to a location to a value? 'position' will need to be something other than int I'm guessing..

如果您需要更多说明,请务必发表评论,对不起&谢谢!

If you need more clarification be sure to comment, sorry in advance & thank you!

推荐答案

您可以使用 Tuple 存储该位置.或者,您可以创建自己的数据结构.

You can use a Tuple to store that position. Or you can create your own data structure.

示例:最后,您将看到如何访问元组项.

Example: at the end you can see how to access tuple items.

var positions = new List<Tuple<int, int>>();

            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    if (n.getPuzzle()[x,y] == 0)
                    {
                        positions.Add(new Tuple<int, int>(x,y));
                        break;
                    }
                }//end y loop
            }//end x loop

            if(positions.Any())
            {
                var xpos = positions[0].Item1;
                var ypos = positions[0].Item2;
            }

这篇关于如何保存多维数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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