我有一个按钮,该按钮应该接受“姓”字符串输入,在目录数组中搜索与该姓相关的“记录”结构,并将该记录输出到列表视图。该目录包含3行字符串记录结构的许多行:姓,名,扩展码。
经过数天的类似SO问题和其他来源的搜索,我拼凑出了最适合我的问题的方法,但是由于某种原因,我的主要搜索方法没有引用我的辅助方法。您能否告诉我您为什么这样认为,如果我设法使参考文献起作用,您是否认为它会起作用,如果没有,还有其他建议吗?
仅供参考,我的if语句有点不规则,因为我的项目要求姓氏不区分大小写并接受部分字符串匹配。
private void SearchSurname()
{
Array.Sort(directory, (x, y) => String.Compare(x.surname, y.surname));
ClearForm();
int surnameIndex = Array.BinarySearch(directory, txtSurname.Text);
if (directory[surnameIndex].surname.ToUpper().Substring(0, txtSurname.Text.Length).Contains(txtSurname.Text.ToUpper()))
{
ListViewItem record = new ListViewItem();
// Send each field in current record to single listview item
record.Text = (Convert.ToString(txtSurname.Text));
record.SubItems.Add(Convert.ToString(txtForename.Text));
record.SubItems.Add(Convert.ToString(txtExtCode.Text));
// Display new record listview item in listview
lvDirectory.Items.Add(record);
}
}
public int BinarySearch(string[] directory, string searchTerm)
{
int first = 0;
int last = directory.Length - 1;
int position = -1;
bool found = false;
int compCount = 0;
searchTerm = txtSurname.Text;
while (found != true && first <= last)
{
int middle = (first + last) / 2;
if (string.Compare(directory[middle], searchTerm, true) == 0)
{
found = true;
position = middle;
compCount++;
}
else if (string.Compare(directory[middle], searchTerm, true) > 0)
{
last = middle;
compCount++;
}
else
{
first = middle;
compCount++;
}
}
return position;
}
编辑:根据Olivier Jacot-Descombes的回答更新的代码:
private void SearchSurname()
{
// Sort directory alphabetically by surname
Array.Sort(directory, (x, y) => String.Compare(x.surname, y.surname));
ClearForm();
// In directory, find line index of search term
int surnameIndex = BinarySearch(directory, txtSurname.Text);
ListViewItem record = new ListViewItem();
// Send each field in current record to single listview item
record.Text = (Convert.ToString(directory[surnameIndex].surname));
record.SubItems.Add(Convert.ToString(directory[surnameIndex].forename));
record.SubItems.Add(Convert.ToString(directory[surnameIndex].extCode));
// Display new record listview item in listview
lvDirectory.Items.Add(record);
}
private int BinarySearch(record[] directory, string searchTerm)
{
int first = 0;
int last = directory.Length - 1;
int surnameIndex = -1;
bool indexFound = false;
// While index not found and there are still points in the array to check
while (indexFound != true && first < last)
{
int middle = (first + last) / 2;
// If surname field in middle record of directory array matches the search term
if (string.Compare(directory[middle].surname, searchTerm, true) == 0)
{
// Index found!
indexFound = true;
surnameIndex = middle;
MessageBox.Show("If 1");
}
// If surname field in middle record of directory array is higher, alphabetically, than the search term
else if(string.Compare(directory[middle].surname, searchTerm, true) > 0)
{
// The next search will be between the first and the current middle records of the array
last = middle;
MessageBox.Show("If 2");
}
// If surname field in middle record of directory array is lower, alphabetically, than the search term
else
{
// The next search will be between the current middle and the highest records of the array
first = middle;
MessageBox.Show("If 3");
}
}
return surnameIndex;
}
您的SearchSurname
方法调用Array.BinarySearch
,这是Array Class的静态方法。如果要调用自己的方法,则必须编写:
int surnameIndex = BinarySearch(directory, txtSurname.Text); // Without "Array."
您正在通过String.Compare
的第三个参数true
使用不区分大小写的比较。
您可以使用
if (string.StartsWith(directory[middle], searchTerm,
StringComparison.CurrentCultureIgnoreCase))
仅搜索名称的开头。但是,如果多个名称匹配,则会出现问题。例如。您正在搜索"smit"
,并且数组中有"Smith"
和"Smithy"
。因此,您必须测试找到的匹配之前和之后的条目,并返回所有匹配的条目作为可能的结果。