【问题标题】:Searching for a string in a part of a multidimensional array c#在多维数组的一部分中搜索字符串c#
【发布时间】:2019-01-12 18:13:39
【问题描述】:

我需要在多维数组的一部分中找到一个字符串的位置。

如果你有:

string[,] exampleArray = new string[3,y]

其中 3 是列数,y 是行数,我需要在完整的 y 行中找到字符串的位置,但只能在第二或第三“列”中。所以我想让我的程序在[1,y] and [2,y]中搜索字符串的位置。

【问题讨论】:

  • 欢迎来到 SO!你能展示你为这个问题所做的任何工作吗?它可能有助于解决问题并告知其他人如何提供帮助。

标签: c# arrays multidimensional-array


【解决方案1】:

一种可能的解决方案是迭代您的行并检查每一行的索引 1 和 2 的每一列。

//Example array to search
string[,] exampleArray = new string[y,3]
//string to search for
string searchedString = "someValue";

//for-loop to iterate the rows, GetLength() will return the length at position 0 or (y)
for(int x = 0; x < exampleArray.GetLength(0); x++)
{
     if(string.Equals(exampleArray[x, 1], searchedString))
     {
         //Do something
         Console.Writeline("Value found at coordinates: " + x.ToString() + ", " + 1.ToString());
     }

     if(string.Equals(exampleArray[x, 2], searchedString))
     {
         //Do something
         Console.Writeline("Value found at coordinates: " + x.ToString() + ", " + 2.ToString());
     }
}

【讨论】:

  • 谢谢!如果您的列比行大得多,是否会被视为错误代码?
  • @Michiel 你所说的“更大”是什么意思,如果你的意思是列多于行,不,我的意思是数据表可能有 20 列,根据它的使用情况可能有 0 - 20 行.
  • 是的,就像我错了的第一个未经编辑的帖子一样,切换列和行
  • @Michiel 一行是某事物的一个实例,比如一个人,一个人可能有很多特征(列),例如(身高、体重、眼睛颜色、头发颜色等)。 .) 如果在某个特定时间唯一存在的人是你和我,那么你将只有 2 行,但会有更多列,所以我认为你不能将其归类为错误代码,它更具情境性。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多