简体   繁体   中英

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.

So far I have:
1. Created an array to store the read all the lines in the file
2. Created a second array to store the final array values
3. Created a third array to store the line splits at , and place values back into second array.

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.
2. use an if statement to determine if userinput is found in index[1] and, if so, store the loop count number. (Here, if no match is found, I will repeat for index[2] and again, if no match found, repeat for 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. (I can do this step but that's hopeless without knowing how to do steps 1 and 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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