C#如何在Array中搜索索引以查找值

时间:2015-11-20 06:33:08

标签: c# arrays search multidimensional-array inputbox

我一直在C#中徘徊,我到目前为止表现不错,但我试图找出如何在多维数组中搜索索引。我有一个阵列,用于每天制作的产品。这通过询问用户输入(使用InputBox)例如显示每周每天产生的产品。第1周周一= 10只熊,周三= 20只熊。第2周周二= 30,周五= 11等。我知道IndexOf,但这是为了找到给定值的索引。我想搜索索引并查找并显示该值。

代码如下:

class Day
{
    private string monday;
    private string tuesday;
    private string wednesday;
    private string thursday;
    private string friday;

    public string Monday
    {
        get { return monday; }
        set { monday = value; }
    }
    public string Tuesday
    {
        get { return tuesday; }
        set { tuesday = value; }
    }
    public string Wednesday
    {
        get { return wednesday; }
        set { wednesday = value; }
    }
    public string Thursday
    {
        get { return thursday; }
        set { thursday = value; }
    }
    public string Friday
    {
        get { return friday; }
        set { friday = value; }
    }
}

private void btnSelect_Click(object sender, EventArgs e)
{
    Day[] week = new Day[4];
    //Week[0] = new Day(); Ask users for input box value
    E.g.Monday = Microsoft.VisualBasic.Interaction.InputBox("Enter the amount of products made on Monday for week 1", "Product Amount"),etc

    //Prints the amounts
    txtOutput.Text += "The product allocation is as follows:" + "\r\n" + "\r\n";
    txtOutput.Text += "                      Mon    Tue     Wed     Thu     Fri  \r\n";

    int weeknum = 0;
    //Iterates through and shows the values for each day of the week.
    foreach (Day days in week)
    {
        weeknum++;
        if (days != null)
        {
            txtOutput.Text += "Week " + weeknum + "         " + days.Monday + "          " + days.Tuesday + "         " + days.Wednesday + "          " + days.Thursday + "         " + days.Friday + "\r\n";
        }
    }
}

3 个答案:

答案 0 :(得分:2)

"我想搜索索引并查找并显示值。"

抱歉,我可能没有回答你的问题,但这听起来像是一个完美的词典用例。

IDictionary<string, int> database = new Dictionary<string, int>
{
    {"Week1 Monday", 10},
    {"Week1 Wednesday", 20}
};

var userinput = "Week1 Monday";
int numberOfBears;
database.TryGetValue(userinput, out numberOfBears);

//numberOfBears = 10

有关详细信息,请参阅http://www.dotnetperls.com/dictionary

答案 1 :(得分:0)

您需要使用Array.GetLowerBound和Array.GetUpperBound方法遍历数组。 Array.IndexOf和Array.FindIndex方法不支持多维数组。

例如:

string[,,] data = new string[3,3,3];
data.SetValue("foo", 0, 1, 2 );

for (int i = data.GetLowerBound(0); i <= data.GetUpperBound(0); i++)
    for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); j++)
        for (int k = data.GetLowerBound(2); k <= data.GetUpperBound(2); k++)
            Console.WriteLine("{0},{1},{2}: {3}", i, j, k, data[i,j,k]);
You might also find the Array.GetLength method and Array.Rank property useful. I recommend setting up a small multidimensional array and using all these methods and properties to get an idea of how they work.

答案 2 :(得分:0)

没有像Array.Find()这样的多维数组的内置函数。

您基本上有两种选择:创建自己的帮助器方法并在那里实现通用搜索模式,或生成与多维数组内容相关的域对象列表。我个人倾向于选择后一种选择。

如果您选择编写辅助方法,它可能看起来像(非常粗略地)这样:

//您可以轻松修改此代码以处理3D数组等。

TMenuItem.OnAdvancedDrawItem

}