-
阵列。BinarySearch(Array,Int32,Int32,Object)方法,用 C#举例
阵列。BinarySearch(Array,Int32,Int32,Object)方法,用 C# 举例
原文:https://www . geeksforgeeks . org/array-binarysearcharay-int 32-int 32-object-method-with-examples-in-c-sharp/
阵列。BinarySearch(Array,int32,int32,Object)方法用于在一维排序数组中的一系列元素中搜索一个值,使用由数组的每个元素和指定的值实现的 IComparable 接口。它只在用户定义的指定边界内搜索。
语法:
public static int BinarySearch(Array array, int index, int length, object value);
参数:
- 数组:是一维数组,我们要在其中搜索一个元素。
- 索引:是要开始搜索的范围的起始索引。
- 长度:是我们要搜索的范围的长度。
- 值:是我们要搜索的值。
返回值:如果找到值,则返回指定的数组中指定的值的索引,否则返回负数。返回值有如下几种不同的情况:
- 如果在数组中没有找到值并且值小于一个或多个元素,则返回的负数是大于值的第一个元素的索引的按位补码。
- 如果找不到值并且值大于数组中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位补数。
- 如果使用非排序数组调用此方法,返回值可能不正确,并且可能返回负数,即使数组中存在值。
异常:
- ArgumentNullException: 如果数组为空。
- rankeexception:如果数组是多维的。
- argumentout of range exception:如果范围小于下限或长度小于 0。
- 参数异常:如果索引和长度没有在数组 或 值中指定有效范围的类型与数组的元素不兼容。
- invalidOperationException:如果值未实现 IComparable 接口,并且搜索遇到未实现 IComparable 接口的元素。
以下程序说明了上述方法:
例 1:
// C# Program to illustrate the use of
// Array.BinarySearch(Array, Int32,
// Int32, Object) Method
using System;
using System.IO;
class GFG {
// Main Method
static void Main()
{
// initializing the integer array
int[] intArr = {42, 5, 7, 12, 56, 1, 32};
// sorts the intArray as it must be
// sorted before using method
Array.Sort(intArr);
// printing the sorted array
foreach(int i in intArr) Console.Write(i + " "
+ "\n");
// intArr is the array we want to find
// and 1 is the starting index
// of the range to search. 5 is the
// length of the range to search.
// 32 is the object to search
int index = Array.BinarySearch(intArr, 1, 5, 32);
if (index >= 0) {
// if the element is found it
// returns the index of the element
Console.Write("Index: " + index);
}
else {
// if the element is not
// present in the array or
// if it is not in the
// specified range it prints this
Console.Write("Element is not found");
}
}
}
Output:
1
5
7
12
32
42
56
Index: 4
示例 2: 如果元素不在数组中,它会打印一个负值,或者它超出了范围。
// C# Program to illustrate the use of
// Array.BinarySearch(Array, Int32,
// Int32, Object) Method
using System;
using System.IO;
class GFG {
// Main Method
static void Main()
{
// initializing the integer array
int[] intArr = {42, 5, 7, 12,
56, 1, 32};
// sorts the intArray as it must be
// sorted before using Method
Array.Sort(intArr);
// printing the sorted array
foreach(int i in intArr) Console.Write(i + " "
+ "\n");
// intArr is the array we want to
// find. and 1 is the starting
// index of the range to search. 5 is
// the length of the range to search
// 44 is the object to search
int index = Array.BinarySearch(intArr, 1, 5, 44);
// as the element is not present
// it prints a negative value.
Console.Write("Index :" + index);
}
}
Output:
1
5
7
12
32
42
56
Index :-7
参考:
- https://docs . Microsoft . com/en-us/dotnet/API/system . array . binary search?view = net framework-4 . 7 . 2 # System _ Array _ binary search _ System _ Array _ System _ int 32 _ System _ int 32 _ System _ Object _
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/27734.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数