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

C# How to search the index in an Array to find the value

我一直在C中闲逛,到目前为止我做得很好,但我正在努力找出如何在多维数组中搜索索引。我有一个数组,用于每天生产的产品。这显示了通过要求用户输入(使用输入框)每天生产的产品,例如第1周星期一=10个熊,星期三=20个熊。第二周星期二=30,星期五=11,等等。我知道指数的含义,但这是为了找到一个给定值的指数。我要搜索索引并查找和显示值。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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:" +"

"
+"

"
;
    txtOutput.Text +="                      Mon    Tue     Wed     Thu     Fri  

"
;

    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 +"

"
;
        }
    }
}


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

抱歉,我可能没有回答您的问题,但这听起来是字典的完美使用案例。

1
2
3
4
5
6
7
8
9
10
11
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。


没有类似array.find()的多维数组内置函数。

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

如果您选择编写助手方法,它可能看起来(非常粗略)如下:

//您可以轻松地修改此代码以处理三维数组等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static class ArrayHelper
{
   public static object FindInDimensions(this object[,] target,
   object searchTerm)
  {
    object result = null;
    var rowLowerLimit = target.GetLowerBound(0);
    var rowUpperLimit = target.GetUpperBound(0);

    var colLowerLimit = target.GetLowerBound(1);
    var colUpperLimit = target.GetUpperBound(1);

    for (int row = rowLowerLimit; row < rowUpperLimit; row++)
    {
        for (int col = colLowerLimit; col < colUpperLimit; col++)
        {
            // you could do the search here...
        }
    }

    return result;
}

}


您需要使用array.getLowerBound和array.getUpperbound方法在数组中循环。array.indexof和array.findindex方法不支持多维数组。

例如:

1
2
3
4
5
6
7
8
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.