首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何迭代锯齿状数组?

如何迭代锯齿状数组?
EN

Stack Overflow用户
提问于 2019-05-13 00:17:17
回答 2查看 0关注 0票数 0

这让我疯狂了好几天。以下为什么不工作?

Dim arr(3, 3) As Integer For y As Integer = 0 To arr.GetLength(0) - 1 For x As Integer = 0 To arr.GetLength(y) - 1 arr(y, x) = y + x Next Next

另外,如果阵列看起来像这样呢?

代码语言:javascript
复制
{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}
EN

回答 2

Stack Overflow用户

发布于 2019-05-13 08:29:14

这个C#代码是获取锯齿状数组中所有项目的组合:

代码语言:javascript
复制
    static void Main(string[] args)
    {
        bool exit = false;
        int[] indices = new int[3] { 0, 0, 0 };
        string[][] vectores = new string[3][];

        vectores[0] = new string[] { "A", "B", "C" };
        vectores[1] = new string[] { "A", "B" };
        vectores[2] = new string[] { "B", "D", "E", "F" };

        string[] item;
        int[] tamaños = new int[3]{vectores[0].GetUpperBound(0), 
            vectores[1].GetUpperBound(0), 
            vectores[2].GetUpperBound(0)};

        while (!exit)
        {
            item = new string[]{ vectores[0][indices[0]],
                    vectores[1][indices[1]],
                    vectores[2][indices[2]]};

            Console.WriteLine("[{0},{1},{2}]={3}{4}{5}", indices[0], indices[1], indices[2], item[0], item[1], item[2]);
            GetVector(tamaños, ref indices, ref exit);
        }
        Console.ReadKey();
    }

    public static void GetVector(int[] tamaños, ref int[] indices, ref bool exit)
    {
        for (int i = tamaños.GetUpperBound(0); i >= 0; i--)
        {
            if (tamaños[i] > indices[i])
            {
                indices[i]++;
                break;
            }
            else
            {
                //ULTIMO ITEM EN EL ARRAY, VALIDAR LAS OTRAS DIMENSIONES SI YA ESTA EN EL ULTIMO ITEM
                if (!ValidateIndexes(tamaños, indices))
                    indices[i] = 0;
                else
                {
                    exit = true;
                    break;
                }
            }
        }
    }

    public static bool ValidateIndexes(int[] tamaños, int[] indices)
    {
        for (int i = 0; i < tamaños.Length; i++)
        {
            if (tamaños[i] != indices[i])
                return false;
        }
        return true;
    }

输出看起来像[0,0,0] = AAB [0,0,1] = AAD [0,0,2] = AAE [0,0,3] = AAF [0,1,0] = ABB [ 0,1,1] = ABD [0,1,2] = ABE [0,1,3] = ABF [1,0,0] = BAB [1,0,1] = BAD [1,0,2] ] = BAE [1,0,3] = BAF [1,1,0] = BBB [1,1,1] = BBD [1,1,2] = BBE [1,1,3] = BBF [2 ,0,0] = CAB [2,0,1] = CAD [2,0,2] = CAE [2,0,3] = CAF [2,1,0] = CBB [2,1,1] = CBD [2,1,2] = CBE [2,1,3] = CBF

票数 0
EN

Stack Overflow用户

发布于 2019-05-13 09:53:17

那么如果我有一个看起来像这样的阵列呢?

代码语言:javascript
复制
{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}

GetLength(1)如何知道每行的长度?

基本上我想要的是...... 一种查找任何给定行中元素数量的方法

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100001144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档