C# 如何检查多维数组是否有值及其索引?
我有一个多维数组:C# 如何检查多维数组是否有值及其索引?,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我有一个多维数组: string[,] array = new string[,] { {"cat", "dog", "plane"}, {"bird", "fish", "elephant"}, }; 我想确定它是否包含一个值,如果是,我需要它的索引,比如说,对于bird 我需要的是 查找是否包含它。 获取它的索引。 获取第二个维度的长度,我不知道如何调用它,并返回一个从第二个元素到该维度最后一个元素的随机值。 所以,如果我说鸟,我希望它在鱼和大象之间给我一条随机的线。 如果
string[,] array = new string[,]
{
{"cat", "dog", "plane"},
{"bird", "fish", "elephant"},
};
我想确定它是否包含一个值,如果是,我需要它的索引,比如说,对于bird
我需要的是
查找是否包含它。
获取它的索引。
获取第二个维度的长度,我不知道如何调用它,并返回一个从第二个元素到该维度最后一个元素的随机值。
所以,如果我说鸟,我希望它在鱼和大象之间给我一条随机的线。
如果它是一个普通数组,我会制作一个简单的
random.Next(1, array.Length);
但是,我不知道如何使用2D数组
谢谢 您需要使用array.GetLength而不是array.Length来获取多维数组中单个维度的长度
遍历数组。如果找到匹配项,请存储当前索引并使用array.GetLength和random类从匹配行中获取随机值
一份清单会更容易处理
如果我从您的原始数据开始,我会将其嵌套在列表中:
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();
用getRandombird正确地调用它会给我带来鱼或象。下面是一个使用多维数组的示例。请注意,在注释中有一个需要处理的边缘情况
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));
}
}
下面是一个不破坏数据结构的示例:
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,以及是否可以从索引和行尾之间的范围中获得一个随机数
如果多维数组可以包含不同大小的行,则还应使用字符串[]。对于字符串[,],数组必须包含大小相同的行。是否可以检查您是否只搜索最左边的列以查找匹配项,然后从其余列生成随机值?或者可以在任何列中进行匹配?
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();
}