【问题标题】:Binary searching a string array not finding searched string C#二进制搜索字符串数组未找到搜索的字符串C#
【发布时间】:2023-03-24 17:37:01
【问题描述】:

我正在尝试使用已在文本框中输入的文本搜索对象数组。

我已经将对象数组转换为字符串数组,但是我仍然没有找到正确的索引

我正在使用 C# 中的内置二进制搜索选项,因为这是已请求的。我无法改变这一点。

如果有人可以提供帮助,那就太好了 - 如果您需要我的任何东西,请不要害怕给我留言。

这是客户数组

Customer[] cust = new Customer[20];

这是客户类的排序方法

private void customerSort()
        {
            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < customerPTR - 1; x++)
                {
                    if (string.Compare(cust[x].GSname, cust[x + 1].GSname) > 0)
                    {
                        customerSwapRoutine(cust[x]);
                    }
                }
            }
        }

和交换例程

private void customerSwapRoutine(Customer book, int x = 0)
        {
            string tempString = cust[x].GSname;
            cust[x].GSname = cust[x + 1].GSname;
            cust[x + 1].GSname = tempString;

            string tempString2 = cust[x].GScID;
            cust[x].GScID = cust[x + 1].GScID;
            cust[x + 1].GScID = tempString2;

            string tempString3 = cust[x].GSlocation;
            cust[x].GSlocation = cust[x + 1].GSlocation;
            cust[x + 1].GSlocation = tempString3;

            string tempString4 = cust[x].GSemail;
            cust[x].GSemail = cust[x + 1].GSemail;
            cust[x + 1].GSemail = tempString4;
        }

这是客户类

class Customer
    {
        private string name, location, email, cID;

        public string GScID
        {
            get { return cID; }
            set { cID = value; }
        }
        public string GSname
        {
            get { return name; }
            set { name = value; }
        }
        public string GSlocation
        {
            get { return location; }
            set { location = value; }
        }
        public string GSemail
        {
            get { return email; }
            set { email = value; }
        }
        public string displayCustomer()
        {
            return GScID + " " + GSname + " " + GSlocation + " " + GSemail;
        }
    }

这里是搜索方法

private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string[] str = new string[cust.Length];

            for(int y = 0; y < cust.Length; y++)
            {
                if(cust[y] == null)
                {
                    Customer nc = new Customer();
                    cust[y] = nc;
                    cust[y].GScID = "";
                    cust[y].GSemail = "";
                    cust[y].GSlocation = "";
                    cust[y].GSname = "";
                }

                str[y] = cust[y].GScID;
            }

            string stringcID = tbCUSTOMERID.Text;

            int found = Array.BinarySearch(str, stringcID);

            if (found < 0)
            {
                MessageBox.Show("Customer doesn't exist");
            }
            else
            {
                MessageBox.Show("Customer found!");
                tbCUSTOMERID.Text = cust[found].GScID;
                tbCUSTOMERNAME.Text = cust[found].GSname;
                tbCITY.Text = cust[found].GSlocation;
                tbEMAIL.Text = cust[found].GSemail;
            }
        }

【问题讨论】:

  • 您确认正在排序吗?因为您没有传递 x 参数,所以我有理由相信此方法将始终查看数组的 0 和 1 元素,而不是在数组中移动。

标签: c# arrays binary-search


【解决方案1】:

如果您查看这部分代码:

for(int y = 0; y < cust.Length; y++)
{
                if(cust[y] == null)
                {
                    Customer nc = new Customer();
                    cust[y] = nc;
                    cust[y].GScID = "";
                    cust[y].GSemail = "";
                    cust[y].GSlocation = "";
                    cust[y].GSname = "";
                }

                str[y] = cust[y].GScID;
}
string stringcID = tbCUSTOMERID.Text;
int found = Array.BinarySearch(str, stringcID);

在执行BinarySearch 之前,您正在将大量新客户对象插入到您的 cust 数组中。这将破坏现有的排序。

documentation

使用指定的比较器在整个排序列表中搜索元素并返回元素的从零开始的索引。

整个数组应该已经在BinarySearch 之前排序。 因此,要么您需要在添加这些新的 Customer 对象后再次对数组进行排序。 或者您应该将这些新的Customer 对象添加到已排序字符串中的正确索引中,以便它可以保持正确的排序。

还有另一个错误,customerSort 函数使用GSname 字段进行排序。但是string[] str 数组由GScID 字段组成。您应该对相同的内容进行排序和搜索。

所以,您的代码有问题。如果您确保排序,那么它应该可以工作。

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多