2016-05-15 72 views
0

我有一个多维数组:C#如何检查多维数组是否有值并且是索引?

string[,] array = new string[,] 
    { 
     {"cat", "dog", "plane"}, 
     {"bird", "fish", "elephant"}, 
    }; 

而且我想,以确定它是否包含一个值,如果是的话,我需要它的指数,让我们说,对“鸟”。

我需要的是

  • 查找,如果它包含它。
  • 获取它的索引。
  • 获取第二个维度的长度(我不知道它是如何调用的)并从第二个元素返回一个随机值到该维度的最后一个维度。

所以,如果我说“鸟”,我希望它给我一个“鱼”和“大象”之间的随机字符串。 如果它是一个正常的阵列我会做一个简单的

random.Next(1, array.Length); 

但是,我不知道如何使用二维数组做到这一点。

谢谢!

+0

我可以检查您是否只在最左边一列搜索匹配项,然后从其余列中生成一个随机值?或者比赛可以在任何列? – Enigmativity

回答

2

您需要使用array.GetLength()而不是array.Length来获取多维数组的单个维度的长度。

遍历数组。如果找到匹配项,则存储当前索引并使用array.GetLengthRandom类从匹配行中获取一个随机值。

Random rnd = new Random(); 

int index = -1; 
string randomWord = ""; 

for(int i = 0; i < array.GetLength(0); i++) 
{ 
    if (array[i,0] == "bird") 
    { 
     index = i; 
     randomWord = array[i,rnd.Next(1, array.GetLength(1))]; 
     break; 
    } 
} 
+0

这是正确的!谢谢! –

+0

不客气@Javier。 –

1

A List<list<string>>会更容易处理。

如果我开始用自己的原始数据,我会做这个巢它在一个列表:

List<List<string>> nested = 
    array 
     .OfType<string>() 
     .Select((x, i) => new { x, i }) 
     .GroupBy(x => x.i/array.GetLength(1), x => x.x) 
     .Select(x => x.ToList()) 
     .ToList(); 

现在我可以写这样的功能:

var random = new Random(); 
Func<string, string> getRandom = x => 
(
    from row in nested 
    where row[0] == x 
    from choice in row.Skip(1).OrderBy(y => random.Next()) 
    select choice 
).FirstOrDefault(); 

getRandom("bird")调用它正确地给我"fish""elephant"

+0

它的工作。谢谢! –

1

下面是一个用多维数组来做你想要的例子。请注意,在评论中有一个边缘情况需要处理。

using System; 

class Program 
{ 
    static string[,] array = new string[,] 
    { 
     { "cat", "dog", "plane" }, 
     { "bird", "fish", "elephant" }, 
    }; 

    static int FindRow(string elem) 
    { 
     int rowCount = array.GetLength(0), 
      colCount = array.GetLength(1); 
     for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) 
     { 
      for (int colIndex = 0; colIndex < colCount; colIndex++) 
      { 
       if (array[rowIndex, colIndex] == elem) 
       { 
        return rowIndex; 
       } 
      } 
     } 
     return -1; 
    } 

    static string PickRandomTail(int rowIndex) 
    { 
     int colCount = array.GetLength(1); 
     int randColIndex = new Random().Next(1, colCount); 
     return array[rowIndex, randColIndex]; 
    } 

    static void Main() 
    { 
     int rowIndex = FindRow("bird"); 
     if (rowIndex < 0) 
     { 
      // handle the element is not found 
     } 
     Console.WriteLine(PickRandomTail(rowIndex)); 
    } 
} 
-1

这里不破坏你的数据结构的例子:

static int IndexOf<T>(T[,] array, T toFind) 
    { 
     int i = -1; 
     foreach (T item in array) 
     { 
      ++i; 
      if (toFind.Equals(item)) 
       break ; 
     } 
     return i; 
    } 

    static string GetRandomString(string[,] array, string toFind) 
    { 
     int lineLengh = array.Length/array.Rank; 
     int index = IndexOf(array, toFind); 
     int lineIndex = index/lineLengh; 

     Random random = new Random(); 
     index = random.Next(lineIndex * lineLengh + 1, (lineIndex + 1) * lineLengh); 
     return array[lineIndex, index % lineLengh]; 
    } 

    // If you want to get a random element between the index and the end of the line 
    // You can replace "bird" by any word you want, 
    // except a word at the end of a line (it will return you the first element of the next line) 
    // static string GetRandomString(string[,] array, string toFind) 
    // { 
    //  int lineLengh = array.Length/array.Rank; 
    //  int index = IndexOf(array, toFind); 

    //  Random random = new Random(); 
    //  index = random.Next(index + 1, (index/lineLengh + 1) * lineLengh); 
    //  return array[index/lineLengh, index % lineLengh]; 
    // } 

    static void Main(string[] args) 
    { 
     string[,] array = new string[,] 
     { 
      {"cat", "dog", "plane"}, 
      {"bird", "fish", "elephant"}, 
     }; 
     Console.WriteLine(GetRandomString(array, "bird")); 
     Console.ReadKey(); 
    } 

是完美的,你应该添加一个检查,如果该指数不为-1,如果你可以从范围内获得一个随机数在索引和行结束之间。


如果多维数组可以包含不同大小的行,还应该使用string [] []。使用字符串[,],您的数组必须包含具有相同大小的行。

相关问题