2016-05-15 63 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 [] []。使用字符串[,],您的數組必須包含具有相同大小的行。

相關問題