C# ArrayList.BinarySearch 小问题 --- 必须是按顺序存储 才可以这样 查找
ArrayList.BinarySearch (Object) | 使用默认的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。 |
ArrayList.BinarySearch (Object, IComparer) | 使用指定的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。 |
ArrayList.BinarySearch (Int32, Int32, Object, IComparer) | 使用指定的比较器在已排序 ArrayList 的某个元素范围中搜索元素,并返回该元素从零开始的索引。 由 .NET Compact Framework 支持。 |
---强调一点 。因为是 2分查找。所以必须是 按照顺序存放值, 否则出错。
有2个方式实现 IComparer 和 类自己 实现一个接口 system 中的
D:\c_\arraylist>Test
person CompareTo(object obj) this person is 4 -- objthis person is 3
person CompareTo(object obj) this person is 2 -- objthis person is 3
person CompareTo(object obj) this person is 104 -- objthis person is 3
find person p3 = new person(3); -3 比较后得不到结果 严重问题
-----
person CompareTo(object obj) this person is 4 -- objthis person is 5
find person p3 = new person(3); 6
-----
person CompareTo(object obj) this person is 4 -- objthis person is 1
person CompareTo(object obj) this person is 2 -- objthis person is 1
find person p1 = new person(1); 0
-----
person CompareTo(object obj) this person is 4 -- objthis person is 104
person CompareTo(object obj) this person is 5 -- objthis person is 104
person CompareTo(object obj) this person is 1 -- objthis person is 104
person CompareTo(object obj) this person is -88 -- objthis person is 104
find person p6 = new person(104); -10 -- 比较后得不到结果 严重问题
0 this person is 1
1 this person is 2
2 this person is 104
3 this person is 3
4 this person is 4
5 this person is 122
6 this person is 5
7 this person is 1
8 this person is -88
using
System;
using System.Collections;
using System.Collections.Generic;
public class Test
{
public class person : IComparable
{
public int age = 0;
public person(int i)
{
this.age = i;
}
public override string ToString()
{
return "this person is " + age;
}
public int CompareTo(object obj)
{
Console.WriteLine(" person CompareTo(object obj) " + this.ToString() + " -- obj" + obj.ToString());
if (obj is person)
{
person temp = (person)obj;
return age.CompareTo(temp.age);
}
throw new ArgumentException("object is not a CompareTo ");
}
}
public static void Main(string[] args)
{
ArrayList list = new ArrayList(300);
person p1 = new person(1);
person p2 = new person(2);
person p3 = new person(3);
person p4 = new person(4);
person p5 = new person(5);
person p0 = new person(-88);
person p6 = new person(104);
person p7 = new person(122);
list.Add(p1);
list.Add(p2);
list.Add(p6);
list.Add(p3);
list.Add(p4);
list.Add(p7);
list.Add(p5);
list.Add(p1);
list.Add(p0);
/**//*
Console.WriteLine(list[1]);
list.Remove(1); // 1 will be object for method input paramt
Console.WriteLine(list[1]);
list.RemoveAt(1);
Console.WriteLine(list[1]);
*/
/**//*
*
* ArrayList list0 = new ArrayList(2);
list0.Add(new person(12));
* list.AddRange(list0);
Console.WriteLine(" 合并集合 AddRange 新的集合都在最后 " + list[2]);
Console.WriteLine(" 老集合的包含的数量 --添加新的以用不修改老的集合 " + list0.Count);
*/
Console.WriteLine("find person p3 = new person(3); " + list.BinarySearch(p3));
Console.WriteLine("-----");
Console.WriteLine("find person p3 = new person(3); " + list.BinarySearch(p5));
Console.WriteLine("-----");
Console.WriteLine(" find person p1 = new person(1); " + list.BinarySearch(p1));
Console.WriteLine("-----");
Console.WriteLine(" find person p6 = new person(104); " + list.BinarySearch(p6));
for (int i = 0; i < list.Count; i++ )
{
Console.WriteLine(i + " " + list[i].ToString() );
}
}
}
/**/ /*
Summary:
Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
Parameters:
x: The first object to compare.
y: The second object to compare.
Return Values:
Value Condition Less than zero x is less than y. Zero x equals y. Greater than zero x is greater than y.
Exceptions:
System.ArgumentException: Neither x nor y implements the System.IComparable interface.-or- x and y are of different types and neither one can handle comparisons with the other.
*/
class personIComparer : System.Collections.IComparer
{
public int Compare(object x, object y) {
Test.person p1 = x as Test.person;
Test.person p2 = y as Test.person;
if ( p1 == null ) {
//??
}
if (p1.age == p2.age ){
return 0;
}else{
if (p1.age > p2.age ) {
return 1;
}else{
return -1;
}
}
}
}
/**/ /*
public class Temperature : IComparable {
/// <summary>
/// IComparable.CompareTo implementation.
/// </summary>
public int CompareTo(object obj) {
if(obj is Temperature) {
Temperature temp = (Temperature) obj;
return m_value.CompareTo(temp.m_value);
}
throw new ArgumentException("object is not a Temperature");
}
// The value holder
protected int m_value;
public int Value {
get {
return m_value;
}
set {
m_value = value;
}
}
public int Celsius {
get {
return (m_value-32)/2;
}
set {
m_value = value*2+32;
}
}
}
*/
using System.Collections;
using System.Collections.Generic;
public class Test
{
public class person : IComparable
{
public int age = 0;
public person(int i)
{
this.age = i;
}
public override string ToString()
{
return "this person is " + age;
}
public int CompareTo(object obj)
{
Console.WriteLine(" person CompareTo(object obj) " + this.ToString() + " -- obj" + obj.ToString());
if (obj is person)
{
person temp = (person)obj;
return age.CompareTo(temp.age);
}
throw new ArgumentException("object is not a CompareTo ");
}
}
public static void Main(string[] args)
{
ArrayList list = new ArrayList(300);
person p1 = new person(1);
person p2 = new person(2);
person p3 = new person(3);
person p4 = new person(4);
person p5 = new person(5);
person p0 = new person(-88);
person p6 = new person(104);
person p7 = new person(122);
list.Add(p1);
list.Add(p2);
list.Add(p6);
list.Add(p3);
list.Add(p4);
list.Add(p7);
list.Add(p5);
list.Add(p1);
list.Add(p0);
/**//*
Console.WriteLine(list[1]);
list.Remove(1); // 1 will be object for method input paramt
Console.WriteLine(list[1]);
list.RemoveAt(1);
Console.WriteLine(list[1]);
*/
/**//*
*
* ArrayList list0 = new ArrayList(2);
list0.Add(new person(12));
* list.AddRange(list0);
Console.WriteLine(" 合并集合 AddRange 新的集合都在最后 " + list[2]);
Console.WriteLine(" 老集合的包含的数量 --添加新的以用不修改老的集合 " + list0.Count);
*/
Console.WriteLine("find person p3 = new person(3); " + list.BinarySearch(p3));
Console.WriteLine("-----");
Console.WriteLine("find person p3 = new person(3); " + list.BinarySearch(p5));
Console.WriteLine("-----");
Console.WriteLine(" find person p1 = new person(1); " + list.BinarySearch(p1));
Console.WriteLine("-----");
Console.WriteLine(" find person p6 = new person(104); " + list.BinarySearch(p6));
for (int i = 0; i < list.Count; i++ )
{
Console.WriteLine(i + " " + list[i].ToString() );
}
}
}
/**/ /*
Summary:
Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
Parameters:
x: The first object to compare.
y: The second object to compare.
Return Values:
Value Condition Less than zero x is less than y. Zero x equals y. Greater than zero x is greater than y.
Exceptions:
System.ArgumentException: Neither x nor y implements the System.IComparable interface.-or- x and y are of different types and neither one can handle comparisons with the other.
*/
class personIComparer : System.Collections.IComparer
{
public int Compare(object x, object y) {
Test.person p1 = x as Test.person;
Test.person p2 = y as Test.person;
if ( p1 == null ) {
//??
}
if (p1.age == p2.age ){
return 0;
}else{
if (p1.age > p2.age ) {
return 1;
}else{
return -1;
}
}
}
}
/**/ /*
public class Temperature : IComparable {
/// <summary>
/// IComparable.CompareTo implementation.
/// </summary>
public int CompareTo(object obj) {
if(obj is Temperature) {
Temperature temp = (Temperature) obj;
return m_value.CompareTo(temp.m_value);
}
throw new ArgumentException("object is not a Temperature");
}
// The value holder
protected int m_value;
public int Value {
get {
return m_value;
}
set {
m_value = value;
}
}
public int Celsius {
get {
return (m_value-32)/2;
}
set {
m_value = value*2+32;
}
}
}
*/