数组内部的顺序搜索问题

本文关键字:问题 顺序搜索 内部 数组 | 更新日期: 2023-09-27 18:18:42

    string[] names = { "Abby", "Bill", "Connie", "David", "Eddy" };
    private void clearButton_Click(object sender, EventArgs e)
    {
        //Clear the array labels
        array1Label.Text = "";
        array2Label.Text = "";
    }
    private void passArrayButton_Click(object sender, EventArgs e)
    {
        //Demo how to pass an array
        //Which really means passing the reference vairable to the array.
        //Create an array
        int[] array1 = {10, 20, 30, 40, 50};
        //Pass array1 to a method to double and dsiplay the array
        DoubleArray(array1);
    }
    private void DoubleArray(int[] theArray1)
    {
        string output = "";
        for (int x = 0; x < theArray1.Length; x++)
        {
            theArray1[x] = theArray1[x] * 2;
        }
        //display the array
        foreach(int element in theArray1)
        {
            output = output + element + "'n";
        }
        array1Label.Text = output;
    }
    private void showNameButton_Click(object sender, EventArgs e)
    {         
        string[] names = { "Abby", "Bill", "Connie", "David", "Eddy" };           
    }     
}

非常困惑如何使用顺序搜索能够输出"David"到标签上,请帮助。这是我的整个程序第一部分就是把所有的数字相乘。请帮助!没有外部输入,只需要遍历"names"数组,挑出"David",并将文本输出到标签中。

数组内部的顺序搜索问题

:)

private void showNameButton_Click(object sender, EventArgs e)
{
    string searchKey ="David";
    string output = "";
    string[] names = { "Abby", "Bill", "Connie", "David", "Eddy"};
    foreach(var name in names)
    {
       if(name == searchKey){
           output = name;
           break;
        }
    }
    array1Label.Text = output;
}         

把除David以外的任何东西放到searchkey中。