- 浏览: 15823 次
- 性别:
- 来自: 北京
最近访客 更多访客>>
最新评论
-
尔今尔后:
小女子 姿色不错呀~·~~
4.24学习笔记 -
索菲亚. ぅ:
xiaomei_zhang_pp 写道个人觉得你对问题描述有些 ...
一个关于javascript的问题 -
xiaomei_zhang_pp:
个人觉得你对问题描述有些不清晰“select节点的选值是否都为 ...
一个关于javascript的问题 -
kimmking:
<div class="quote_title ...
4.24学习笔记 -
索菲亚. ぅ:
<div class="quote_title ...
4.24学习笔记
C#中
数组长度表示用.length;
arraylist集合中对象个数表示用.count;
c#数组的声明方式
int[] intArray; int[] intArray = {99,34,56,12,1}; int[] intArray = new int[5]; int[] intArray = new int[varsize]; int[] intArray = new int[5]{99,34,56,12,1};
从旧数组复制到新数组,2表示新数组开始位置
oldArray.CopyTo(newArray, 2);
循环迭代出Hashtable中对象
错误:
foreach(Person p in peopleHashtable) // Will compile but not run! { Response.Write(p.FullName + "<BR/>"); }
正确:
foreach (DictionaryEntry de in peopleHashtable) { Response.Write(de.Key.ToString() + ":" + ((Person)de.Value).FullName + "<BR/>"); }
SortedList
1创建空的SortedList 2手动迭代Hashtable的DictionaryEntry(Hashtable是无须存放的) 3把它们添加到SortedList(添加过程中自动排序) 实例: SortedList peopleSortedList = new SortedList(peopleHashtable); foreach (Person p in peopleSortedList.Values) { Response.Write(p.FullName + "<BR/>"); } Response.Write("Index of Srinivasa: " + peopleSortedList.IndexOfKey("ss"));
Queues适合于按照对象进入的顺序来存储它们(先进先出),
而Stack是一个先进后出结构。
但要取的话必须删一个取一个
BitArray
管理一个布尔值数组或者集合使用
强类型化的集合如PersonList类
注意:强制类型转换隐藏在了属性访问器中,属性的返回值是强类型化的。(这里this是C#属性访问器的关键字)
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections; /// <summary> ///PersonList 的摘要说明 /// </summary> public class PersonList:System.Collections.IEnumerable { public PersonList() { // //TODO: 在此处添加构造函数逻辑 // } private ArrayList innerList = new ArrayList(); public void Add(Person aPerson) { innerList.Add(aPerson); } public void Remove(Person aPerson) { innerList.Remove(aPerson); } public int Count { get { return innerList.Count; } } //Get/Set element at given index public Person this[int index] { get { return (Person)innerList[index]; } set { innerList[index] = value; } } public IEnumerator GetEnumerator() { return innerList.GetEnumerator(); } }
泛型的Hashtable 和Hashtable区别,读取的时候不需要强制类型转化
LinkedList<T>是一个强类型化的双链接表,该链接表中的每个节点都指向前一个节点和后一个节点
问题:谁能帮我讲解下BinarySearch方法?小女谢谢了。。
评论
4 楼
尔今尔后
2012-02-03
小女子 姿色不错呀~·~~
3 楼
kimmking
2009-04-29
<div class="quote_title">索菲亚. ぅ 写道</div><div class="quote_div"><div class="quote_title">kimmking 写道</div>
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p></div><br/>用在已经排序过了的时候,特别是数据量比较大的时候,可以提高查找效率。
一般不用,也许你一辈子用不着。
----mm加我qq 3694826
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p></div><br/>用在已经排序过了的时候,特别是数据量比较大的时候,可以提高查找效率。
一般不用,也许你一辈子用不着。
----mm加我qq 3694826
2 楼
索菲亚. ぅ
2009-04-29
<div class="quote_title">kimmking 写道</div>
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p>
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p>
1 楼
kimmking
2009-04-28
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
if (array==null)
throw new ArgumentNullException("array");
int lb = array.GetLowerBound(0);
if (index < lb || length < 0)
throw new ArgumentOutOfRangeException((index<lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - (index - lb) < length)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (array.Rank != 1)
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
if (comparer == null) comparer = Comparer.Default;
if (comparer == Comparer.Default) {
int retval;
bool r = TrySZBinarySearch(array, index, length, value, out retval);
if (r)
return retval;
}
int lo = index;
int hi = index + length - 1;
Object[] objArray = array as Object[];
if(objArray != null) {
while (lo <= hi) {
// i might overflow if lo and hi are both large positive numbers.
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(objArray[i], value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
else {
while (lo <= hi) {
int i = GetMedian(lo, hi);
int c;
try {
c = comparer.Compare(array.GetValue(i), value);
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (c == 0) return i;
if (c < 0) {
lo = i + 1;
}
else {
hi = i - 1;
}
}
}
return ~lo;
}
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
发表评论
-
ASP.NET 页面事件顺序和生命周期
2009-04-27 14:04 1449生命周期/Files/DavidFan/asp.net.lif ... -
asp.net页面事件执行顺序(2)
2009-04-27 14:03 2015当页面被提交请求 ... -
asp.net页面事件执行顺序(1)
2009-04-27 14:00 1254using System; using System.Dat ... -
4.17-4.20学习小记
2009-04-27 13:56 707customValidator aspx: <scr ... -
4.16学习笔记
2009-04-20 22:21 808存储过程: CREATE PROCEDURE [inser ... -
ASP.NET 2.0中的脚本回调(Script Callback) [转]
2009-04-15 21:39 1217原作者:Dino Esposito 出 ... -
4.15学习笔记
2009-04-15 21:36 863--备份(这两条语句一起才算是完整的备份,第一条是备份数据库, ... -
ASP.NET2.0实现无刷新客户端回调
2009-04-15 21:26 1018概述 在Asp.net网页的默认模型中,用户与页进行 ...
相关推荐
4.24师德师风政治学习笔记.pdf
ashly 矩阵及4.24C 处理器软件
JINK_V8_4.24f固件
联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载...
P2P终结者 4.24,可以控制网速,很实用。
从官网下载还需要注册登录,很是麻烦,这里提供了官网原版下载的STM32CubeMX 4.24.0
这个p2p终结者4.24版本,此版本为最新版
libev4.24下载
驱动软件!jlink的驱动!用于j-link的使用!
UnrealEngine 4.24源码。在Unrealengine文件夹下./Setup.sh && ./GenerateProjectFiles.sh && make
最新的蓝牙开发辅助工具。用于Nordic或者其他蓝牙设备的扫描,OTA,查看数据包和属性、服务等各种开发参数。调试设备,测试设备。解压后直接安装使用。
libev-4.24,鄙视那个资源分放6分的,还是个老版本,欢迎下载
自用
UE4.24.3版本源码,和Github发布版本相同,下载时间2022.7.18。
nordic nrfconnectv4.24.3_downcc.com.apk
cFosSpeed是一款具有很多功能DSL modem和路由器驱动软件,可以充分使用你的ISDN和DSL连接。 程序特点: -高级Traffic Shaping (通信量调整功能); -用于DSL 调制解调器和路由器; -与通用的 PPPoE 驱动器...
亲测有用的OMRON plc9.3序列号
pmd-bin-4.24
nRF.Connect.4.24.3.apk,nordic,bluetooth,tool
ArubaInstant_Centaurus_6.5.4.24_84938适用于以下aruba AP升级Aruba 270 Series Outdoor Access PointsAruba 228 Series Hardened Access PointsAruba 220 Series Campus Access PointsAruba 210 Series Campus ...