Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在条件中进行二进制搜索?_C# - Fatal编程技术网

C# 如何在条件中进行二进制搜索?

C# 如何在条件中进行二进制搜索?,c#,C#,我在这里有一段代码,用来在调用时创建ID Randome,然后添加到ArrayList,但是我想检查我是否已经有相同的ID不添加到ArrayList,我已经使用BinarySearch来检查结果 但看起来有什么不对劲 public delegate void DESetUp(); public static DESetUp IdSetUP = delegate () { ArrayList valID = new ArrayList();

我在这里有一段代码,用来在调用时创建ID Randome,然后添加到ArrayList,但是我想检查我是否已经有相同的ID不添加到ArrayList,我已经使用BinarySearch来检查结果 但看起来有什么不对劲

    public delegate void DESetUp();

    public static DESetUp IdSetUP = delegate ()
    {
        ArrayList valID = new ArrayList();

        Func<int> getID = () => new Random().Next(1, 5);

        int Result = getID();

        if (Result == valID.BinarySearch(Result))
        {
            valID.Add(Result);
            Console.WriteLine("AddSuccessful");
        }
        else
        {
            Console.WriteLine("AddFailed");
        }


        foreach (var item in valID)
        {
            Console.WriteLine("your id is : {0}", item);

        }
    };
public委托void DESetUp();
公共静态DESetUp IdSetUP=delegate()
{
ArrayList valID=新的ArrayList();
Func getID=()=>newrandom().Next(1,5);
int Result=getID();
if(Result==valID.BinarySearch(Result))
{
有效。添加(结果);
Console.WriteLine(“添加成功”);
}
其他的
{
Console.WriteLine(“添加失败”);
}
foreach(var项处于有效状态)
{
WriteLine(“您的id是:{0}”,item);
}
};
谢谢

试试这个:

使用列表而不是ArrayList: 始终需要使用BinarySearch对值进行排序

        List<int> valID = new List<int>();

        Func<int> getID = () => new Random().Next(1, 10);

        int Result = getID();
        // here need to sort the list
        valID.Sort();

        if (valID.BinarySearch(Result) < 0) // if the position of value is lles than zero the value does not exists
        {
            valID.Add(Result);
            Console.WriteLine("AddSuccessful");
        }
        else
        {
            Console.WriteLine("AddFailed");
        }
List valID=新列表();
Func getID=()=>newrandom().Next(1,10);
int Result=getID();
//这里需要对列表进行排序
valID.Sort();
if(valID.BinarySearch(Result)<0)//如果值的位置小于零,则该值不存在
{
有效。添加(结果);
Console.WriteLine(“添加成功”);
}
其他的
{
Console.WriteLine(“添加失败”);
}

我已经取得了很大的成绩

我协助了代表名单

public class MainClass
{

                           //assist delegate List<int>
    public delegate void DESetUp(List<int> DLint);
                                          //assist delegate List<int>
    public static DESetUp IdSetUP = delegate (List<int> valID)
    {

        Func<int> getID = () => new Random().Next(1, 3);

        int Result = getID();

        // here need to sort the list
        valID.Sort();

        if (valID.BinarySearch(Result) < 0) // if the position of value is lles than zero the value does not exists
        {
            valID.Add(Result);
            Console.WriteLine("AddSuccessful");
        }
        else
        {
            Console.WriteLine("AddFailed");
        }
        foreach (int item in valID)
        {
            Console.WriteLine(item);


        }

    };




    static void Main(string[] args)
    {
        //assist List<int> ID
        List<int> ID = new List<int>();

        //ADD delegate + List<int> + IdSetUP
        IdSetUP(ID);
        IdSetUP(ID);
        IdSetUP(ID);
        IdSetUP(ID);


    }

}
public类MainClass
{
//协助代表名单
公共代表无效解除设置(列表DLint);
//协助代表名单
public static DESetUp IdSetUP=委托(列表有效)
{
Func getID=()=>newrandom().Next(1,3);
int Result=getID();
//这里需要对列表进行排序
valID.Sort();
if(valID.BinarySearch(Result)<0)//如果值的位置小于零,则该值不存在
{
有效。添加(结果);
Console.WriteLine(“添加成功”);
}
其他的
{
Console.WriteLine(“添加失败”);
}
foreach(有效的int项)
{
控制台写入线(项目);
}
};
静态void Main(字符串[]参数)
{
//协助列表ID
列表ID=新列表();
//添加代理+列表+ID设置
IdSetUP(ID);
IdSetUP(ID);
IdSetUP(ID);
IdSetUP(ID);
}
}

谢谢大家

如果您不想要重复的内容,那么使用HashSet怎么样?或者只需执行
foreach(valID.Distinct()中的var项)
BinarySearch
返回匹配的位置,而不是匹配的值。阅读文档页面,它会准确地告诉您如何区分“在此位置找到的项目”和“添加项目时应放置项目的位置”。另外,使用一个泛型集合,让ArrayList在它所属的历史垃圾箱中消失。如果(valID.BinarySearch(valID,Result)){valID.Add(Result);Console.WriteLine(“AddSuccessful”);}无法从INT转换,则如何处理?不要使用
ArrayList
,这是泛型之前的遗留问题。使用
List
Hello谢谢您的回答,但是我在主IdSetUP中调用了一个错误,比如IdSetUP();但是,如果我在主4时间再次调用它,它不工作的权利,这是不检查!!