简体   繁体   English

二进制搜索字符串数组未找到搜索字符串 C#

[英]Binary searching a string array not finding searched string C#

I'm trying to search an object array with text that has been typed into a text box.我正在尝试使用已输入文本框中的文本搜索对象数组。

I have converted the object array to a string array however, I'm still not having any luck with finding the correct index我已经将对象数组转换为字符串数组,但是我仍然没有找到正确的索引

I'm using the inbuilt binary search option within c# as that has been requested.我按照要求在 c# 中使用内置的二进制搜索选项。 I cannot change from this.我无法改变这一点。

If anyone can help that would be great - If you need anything from me don't be afraid to drop me a comment.如果有人可以提供帮助,那就太好了 - 如果您需要我的任何东西,请不要害怕给我留言。

Here's the customer array这是客户数组

Customer[] cust = new Customer[20];

Here's the sorting method for the customer class这里是customer类的排序方法

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]);
                    }
                }
            }
        }

and the swap routine和交换例程

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;
        }

Here's the customer class这是客户类

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;
        }
    }

Here's the search method这是搜索方法

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;
            }
        }

If you look at this part of your code:如果您查看代码的这一部分:

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

You are inserting a lot of new Customer objects into your cust array just before doing a BinarySearch .在执行BinarySearch之前,您将许多新的 Customer 对象插入到您的 cust 数组中。 This will break the existing sorting.这将打破现有的排序。

See the documentation查看文档

Searches the entire sorted List for an element using the specified comparer and returns the zero-based index of the element.使用指定的比较器在整个排序列表中搜索元素,并返回元素的从零开始的索引。

The whole array should be sorted already just before the BinarySearch .整个数组应该在BinarySearch之前已经排序。 So, either you need to sort your array again after adding these new Customer objects.因此,您要么需要在添加这些新的Customer对象后再次对数组进行排序。 Or you should add these new Customer objects to the correct index in the sorted strings, so it can keep its correct sorting.或者您应该将这些新的Customer对象添加到已排序字符串中的正确索引中,以便它可以保持正确的排序。

There is another bug, the customerSort function sorts using GSname field.还有另一个错误, customerSort函数使用GSname字段进行排序。 But string[] str array consists of GScID fields.但是string[] str数组由GScID字段组成。 You should sort and search the same things.您应该对相同的内容进行排序和搜索。

So, your code is buggy.所以,你的代码有问题。 If you make sure of sorting, then it should work.如果您确定排序,那么它应该可以工作。

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

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