Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用累进索引从矩阵中获取值?_C#_.net_List_Matrix - Fatal编程技术网

C# 如何使用累进索引从矩阵中获取值?

C# 如何使用累进索引从矩阵中获取值?,c#,.net,list,matrix,C#,.net,List,Matrix,假设我有这个矩阵: public static List<List<string>> WallPattern1550 = new List<List<string>> { new List<string> { "3", "2", "1", "1", "2" }, new List<string> { "1", "2", "2" }, new List<string> { "2", "1"

假设我有这个矩阵:

public static List<List<string>> WallPattern1550 = new List<List<string>>
{ 
    new List<string> { "3", "2", "1", "1", "2" },
    new List<string> { "1", "2", "2" },
    new List<string> { "2", "1", "2" },
    new List<string> { "2", "2", "1" },
    new List<string> { "2", "1", "1", "1" },
    new List<string> { "1", "2", "1", "1" },
    new List<string> { "1", "1", "2", "1" },
    new List<string> { "1", "1", "1", "2" }
};
公共静态列表WallPattern1550=新列表
{ 
新名单{“3”、“2”、“1”、“1”、“2”},
新列表{“1”、“2”、“2”},
新列表{“2”、“1”、“2”},
新列表{“2”、“2”、“1”},
新列表{“2”、“1”、“1”、“1”},
新列表{“1”、“2”、“1”、“1”},
新列表{“1”、“1”、“2”、“1”},
新列表{“1”、“1”、“1”、“2”}
};
我很快想要16°的值(我指的是索引),也就是
1
(WallPattern1550[3][1]),我该怎么做


如何将16转换为3,1?

可以这样做:

WallPattern1550.SelectMany(x => x).Skip(15).First();
但就我个人而言,我会使用一种扩展方法:

public static T GetValueAt<T>(this List<List<T>> source, int index)
{
     int counter = 0;
     foreach(var list in source)
        foreach (var x in list)
        {
             counter++;
             if (counter == index) return x;
        }

     return default(T);
}
如果要获取索引,可以使用以下另一种扩展方法:

public static void FindCoordinates<T>(this IEnumerable<IEnumerable<T>> source, int count, out int x, out int y)
{
     x = 0;
     y = 0;
     int counter = 0;

     foreach (var list in source)
     {
         y = 0;
         foreach (var z in list)
         {
              if (counter == count) break;
              y++;
              counter++;
         }
         if (counter == count) break;
         x++;
     }    
}

可以这样做:

WallPattern1550.SelectMany(x => x).Skip(15).First();
但就我个人而言,我会使用一种扩展方法:

public static T GetValueAt<T>(this List<List<T>> source, int index)
{
     int counter = 0;
     foreach(var list in source)
        foreach (var x in list)
        {
             counter++;
             if (counter == index) return x;
        }

     return default(T);
}
如果要获取索引,可以使用以下另一种扩展方法:

public static void FindCoordinates<T>(this IEnumerable<IEnumerable<T>> source, int count, out int x, out int y)
{
     x = 0;
     y = 0;
     int counter = 0;

     foreach (var list in source)
     {
         y = 0;
         foreach (var z in list)
         {
              if (counter == count) break;
              y++;
              counter++;
         }
         if (counter == count) break;
         x++;
     }    
}

可以这样做:

WallPattern1550.SelectMany(x => x).Skip(15).First();
但就我个人而言,我会使用一种扩展方法:

public static T GetValueAt<T>(this List<List<T>> source, int index)
{
     int counter = 0;
     foreach(var list in source)
        foreach (var x in list)
        {
             counter++;
             if (counter == index) return x;
        }

     return default(T);
}
如果要获取索引,可以使用以下另一种扩展方法:

public static void FindCoordinates<T>(this IEnumerable<IEnumerable<T>> source, int count, out int x, out int y)
{
     x = 0;
     y = 0;
     int counter = 0;

     foreach (var list in source)
     {
         y = 0;
         foreach (var z in list)
         {
              if (counter == count) break;
              y++;
              counter++;
         }
         if (counter == count) break;
         x++;
     }    
}

可以这样做:

WallPattern1550.SelectMany(x => x).Skip(15).First();
但就我个人而言,我会使用一种扩展方法:

public static T GetValueAt<T>(this List<List<T>> source, int index)
{
     int counter = 0;
     foreach(var list in source)
        foreach (var x in list)
        {
             counter++;
             if (counter == index) return x;
        }

     return default(T);
}
如果要获取索引,可以使用以下另一种扩展方法:

public static void FindCoordinates<T>(this IEnumerable<IEnumerable<T>> source, int count, out int x, out int y)
{
     x = 0;
     y = 0;
     int counter = 0;

     foreach (var list in source)
     {
         y = 0;
         foreach (var z in list)
         {
              if (counter == count) break;
              y++;
              counter++;
         }
         if (counter == count) break;
         x++;
     }    
}

假设您正在查找值
1
,而不是索引
3,1
。问题是后者,但似乎有理由认为他真的想要前者。但是
16
对应的
[4][2]
是我错了还是输入错误?假设您查找的是值
1
,而不是索引
3,1
。问题是后者,但似乎有理由认为他真的想要前者。但是
16
对应的
[4][2]
是我错了还是输入错误?假设您查找的是值
1
,而不是索引
3,1
。问题是后者,但似乎有理由认为他真的想要前者。但是
16
对应的
[4][2]
是我错了还是输入错误?假设您查找的是值
1
,而不是索引
3,1
。问题是后者,但似乎有理由认为他真的想要前者。但是
16
对应的
[4][2]
是我错了还是打字错误?