简体   繁体   English

搜索多维数组

[英]Search multi-dimensional array

I have a multi-dimensional array in a text file:我在文本文件中有一个多维数组:

a,1,2,3  
b,4,5,6  
c,7,8,9  
d,10,11,12

I want to input one of the numbers, search the array, and display the corresponding letter for the row the number appears in.我想输入其中一个数字,搜索数组,并显示该数字所在行的相应字母。

Any help would be greatly appreciated.任何帮助将不胜感激。

Edit:编辑:

I have a csv file containing the information given above.我有一个包含上述信息的 csv 文件。

So far I have:到目前为止,我有:
1. Created an array to store the read all the lines in the file 1.创建一个数组来存储读取的文件中的所有行
2. Created a second array to store the final array values 2. 创建第二个数组来存储最终的数组值
3. Created a third array to store the line splits at , and place values back into second array. 3. 创建了第三个数组来存储在 处的行拆分,并将值放回第二个数组中。

This is the code:这是代码:

string[] as_FirstArray = System.IO.File.ReadAllLines("PartNumbersFile.csv");
string[,] as_SecondArray = new string[4, as_FirstArray.Length];
string[] as_ThirdArray;

string s_Input = Console.ReadLine();

for (int i_Count1 = 0; i_Count1 < as_FirstArray.Length; i_Count1++)
{
    as_ThirdArray = as_FirstArray[i_Count1].Split(',');

    as_SecondArray[0, i_Count1] = as_ThirdArray[0];
    as_SecondArray[1, i_Count1] = as_ThirdArray[1];
    as_SecondArray[2, i_Count1] = as_ThirdArray[2];
    as_SecondArray[3, i_Count1] = as_ThirdArray[3];
}

And now I'm totally stuck.现在我完全被困住了。 I have been told that, from here, I need to:有人告诉我,从这里开始,我需要:
1. use a for loop on as_SecondArray index[1] from first row to last row. 1. 对 as_SecondArray index[1] 从第一行到最后一行使用 for 循环。
2. use an if statement to determine if userinput is found in index[1] and, if so, store the loop count number. 2. 使用 if 语句来确定是否在 index[1] 中找到了用户输入,如果是,则存储循环计数。 (Here, if no match is found, I will repeat for index[2] and again, if no match found, repeat for index[3].) (在这里,如果没有找到匹配项,我将重复 index[2],如果没有找到匹配项,我将重复 index[3]。)
3. use another for loop and if statement on index[0] to match the count number from the found match, and display the corresponding entry. 3. 在 index[0] 上使用另一个 for 循环和 if 语句来匹配找到的匹配项中的计数,并显示相应的条目。 (I can do this step but that's hopeless without knowing how to do steps 1 and 2.) (我可以做这一步,但如果不知道如何做第 1 步和第 2 步,那是没有希望的。)

I don't how to specify a particular index as the loop target.我不知道如何将特定索引指定为循环目标。 Or where I place the loop in relation to the loop I already have - inside it or not.或者我将循环放置在与我已经拥有的循环相关的位置 - 无论是否在其中。

Edit:编辑:

THANK YOU TO WHOEVER EDITED MY POST AND I HAVE NOW DELETED MY ANSWER :-)感谢编辑我帖子的人,我现在已经删除了我的答案:-)

After A LOT more time I have made a little progress.经过更多的时间,我取得了一些进展。 I have figured out how to loop a particular index and identify if userinput is found in that index:我已经弄清楚如何循环特定索引并确定是否在该索引中找到了用户输入:

        for (int i_Count2 = 0; i_Count2 < as_SecondArray.GetLength(1); i_Count2++)
        {
            for (int i_Count3 = 0; i_Count3 < as_SecondArray.GetLength(0); i_Count3++)
            {
                if (as_SecondArray[1, i_Count3].Equals(s_Input))
                {
                    s_Found = as_SecondArray[1, i_Count3];
                }
                else { lblOutput.Text = "not found"; }
            }
        }

But I am stuck at how to retrieve the i_Count3 loop number.但我被困在如何检索 i_Count3 循环编号。

Don't create multidimensional array instead create a Dictionary or Hashtable where key will be the number and value will be the letter.不要创建多维数组,而是创建一个字典或哈希表,其中键是数字,值是字母。 Now you can easily search the number and find associated letter with the search time of O(1).现在您可以轻松搜索数字并找到搜索时间为 O(1) 的相关字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM