通过BinarySearch搜索字符串 [英] search string by BinarySearch
问题描述
大家好!我的代码有问题
我使用StreamReader和StreamWriter读取表单文件并将其添加到Array.当我比较2个数组中的2个字符串时,它无法比较Array1和Array2.
Hello everybody! I have a problem about my code
I use StreamReader and StreamWriter to read form file and add to Array. when I compare 2 string in 2 Array, it can''t compare Array1 and Array2.
Array 1: banana, potato, tomato...
Array 2: David, John, David...
我想要找到字符串"David"时显示David的商品为make.我在Array2 [0]和Array2 [2]上有2个字符串"David",因此结果将是比较位置并显示在Array1 [0]和Array1 [2]上.
将显示此结果(如果为true):香蕉,番茄
I want when I find string "David" then display product of David was make. I have 2 string "David" at Array2[0] and Array2[2], so result will be compare position and display at Array1[0] and Array1[2].
This result will display (if true): banana, tomato
public void Search()
{
Console.Write("Enter string to search: ");
string a= Console.ReadLine();
for (int i=0;i<100;i++) //100 is limit of array
{
if(string.Compare(a,Array2[i])==0) //check Array
Console.WriteLine(Array[i]);
}
}
只是一个例子,请使用BinarySearch!
什么是指数?
that''s a example, Please use BinarySearch!
what''s mean Index? how to creat Index?
推荐答案
要执行二进制搜索,您必须订购Array2
项(并保持Array
同步). br/>
In order to perform a binary search you''ve to order theArray2
items (and keepArray
sinchronized).
什么是索引?如何创建索引?
what''s mean Index? how to creat Index?
我猜这是排序"数组,即
I guess it is the ''ordering'' array, i.e. an array such that
Array2[Index[i]] < Array2[Index[i+1]]
成立.
:)
holds true.
:)
示例:
Array1:"1","2","1","3","1"
Array2:"A","B","A","A","E"
我需要编写代码以从Array1搜索并显示Array2(如果为true).
Example:
Array1: "1", "2", "1", "3", "1"
Array2: "A", "B", "A", "A", "E"
I need write a code to search from Array1 and result display Array2 (if true).
public void FindCity()
{
Console.Clear();
ReadFile();
Console.Write("Enter City to search : ");
string st = Console.ReadLine().ToUpper();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\nProduct of " + st.ToUpper() + ":\n");
Console.ForegroundColor = ConsoleColor.White;
for (int i = 0; i <= countC; i++)
{
if (string.Compare(st, City[i]) == 0)
Console.WriteLine(Sale[i]);
}
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\nEnter any key to return Menu");
Console.ReadKey();
}
但是现在,我有一个新问题.当您在Array1中搜索字符串时:"1",将显示"A","A","E".我希望它显示1次. ("A"和"E")
请引导我编写代码.
和索引?如何使用索引?
But now, I have a new problem. When you search string in Array1: "1", that will be dislplay "A", "A", "E". I want it display 1 time. ("A" and "E")
Please guide me to write code.
And Index? how can I use Index?
这篇关于通过BinarySearch搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!