JUST DO IT ~
我只想當個程序員
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;
}
}
}
*/
posted on 2008-02-25 21:08
小高
閱讀(1639)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
<
2008年2月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
統計
隨筆 - 341
文章 - 0
評論 - 50
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
(352)
Architecture 架構設計(9)
(rss)
Build構建|git版本控制(5)
(rss)
C(56)
(rss)
Code 風格|優化|設計模式|數據結構(7)
(rss)
Concurrency 并發(6)
(rss)
Distribution分布式(2)
(rss)
DotNet(29)
(rss)
Exception 異常處理(10)
(rss)
FRONT 前端(2)
(rss)
GUN命令(13)
(rss)
HTML JS CSS
(rss)
J2EE(5)
(rss)
java基礎(13)
(rss)
leetcode(1)
(rss)
Linux(33)
(rss)
Mac OSX(23)
(rss)
MQ 消息隊列(2)
(rss)
Network| Socket | 進程間通訊(12)
(rss)
Oracle(68)
(rss)
Ruby on Rails(6)
(rss)
Study 學習的藝術(1)
(rss)
Talk 論戰
(rss)
其他(20)
(rss)
工作環境搭建(21)
(rss)
敏捷|項目|團隊|管理|版本|(4)
(rss)
未完成(4)
(rss)
收藏夾
(19)
實用資料(19)
(rss)
關注的blog
Anders-Hejlsberg
awesome資源
gitbook
POSIX Threads Programming
SystemProgramming
UNIX Specification 2
免費的編程中文書籍索引
劉未鵬
開源圖書
徐宥
武劍鋒
手冊
C 語言編程透視
C_learn_code_the_hardway
C10K
cpp手冊
FreeBSD使用手冊
Git Community Book 中文版
hacker news
http://www.tldp.org/LDP/abs/html/index.html
leetcode習題書
Linux中文man在線手冊
Linux和Unix安全編程HOWTO
Mandrakelinux手冊
POSIX Threads for Windows
Programming Ruby實用程序員指南
ruby w3c
search
Shell 編程范例
Shell 編程范例
socket error msdn
TCP 和 UDP 性能調整AIX
unix_socket論壇
w3schools
七月算法
內核分析2003,KernelAnalysis-HOWTO
命令行的藝術
在線編輯markdown文本
開源軟件架構
期貨中英文
調試開源書
通訊百科
鳥哥BASH
搜索
積分與排名
積分 - 301148
排名 - 193
最新評論
1.?re: eclipse tomcat 配置遇到問題
評論內容較長,點擊標題查看
--小高
2.?re: mac 使用總結
不會用mac的飄過。
--京山游俠
3.?re: ireport sql where List in 多個值 jasper
評論內容較長,點擊標題查看
--lul
4.?re: c++ 一些經典論文
評論內容較長,點擊標題查看
--小高
5.?re: redhat 6.5 安裝 codeblock 13.12
你好,我實在是安裝不上呀,能不能幫幫我,謝謝啦 qq:1669941649
--hanrui
閱讀排行榜
1.?oracle trunc(sysdate ,'dd') 日期 (20226)
2.?ibm thinkpad 鍵盤 設置(7471)
3.?oracle 數字 不要 科學計數法 顯示 。(7295)
4.?解決 ghost 安裝 sata 方式轉換 AHCI 方式 Intel Matrix Storage 藍屏 此計算機未達到安裝此軟件的最低要求 (6294)
5.?數組集合 type 類型 is table of 表 %rowtype index by binary_integer;(6102)
評論排行榜
1.?sqlplus 初始化 login.sql (6)
2.?oracle trunc(sysdate ,'dd') 日期 (5)
3.?ibm thinkpad 鍵盤 設置(3)
4.?sqlplus ip 連接 簡單連接(3)
5.? plsql -develop 的測試 procedure 是這樣 實現的..... 綁定變量 未定義 :變量名(2)
Powered by:
BlogJava
Copyright © 小高
主站蜘蛛池模板:
免费观看无遮挡www的视频
|
无码乱人伦一区二区亚洲
|
国产四虎免费精品视频
|
精品一区二区三区免费观看
|
午夜在线免费视频
|
国产亚洲精品成人AA片
|
91亚洲精品第一综合不卡播放
|
亚洲精品动漫人成3d在线
|
久久久久国色AV免费观看性色
|
久久午夜夜伦鲁鲁片免费无码
|
我要看免费的毛片
|
97视频免费观看2区
|
久久嫩草影院免费看夜色
|
亚洲AV第一成肉网
|
亚洲午夜福利在线视频
|
亚洲日韩乱码中文无码蜜桃
|
九月丁香婷婷亚洲综合色
|
亚洲欧洲日产国产最新
|
亚洲成亚洲乱码一二三四区软件
|
成人亚洲综合天堂
|
国产在线a不卡免费视频
|
日韩高清在线免费观看
|
妞干网在线免费观看
|
免费av欧美国产在钱
|
无码国产精品久久一区免费
|
亚洲国产成人高清在线观看
|
中文字幕专区在线亚洲
|
久久久久国产亚洲AV麻豆
|
亚洲福利精品电影在线观看
|
四虎永久成人免费
|
亚洲成AV人网址
|
亚洲成a人片在线观看老师
|
亚洲 无码 在线 专区
|
亚洲国产精品人人做人人爱
|
爱情岛论坛网亚洲品质自拍
|
久久精品国产亚洲Aⅴ香蕉
|
在线播放亚洲第一字幕
|
久久亚洲国产午夜精品理论片
|
国产亚洲精品美女久久久
|
亚洲国产成人久久精品影视
|
亚洲美女视频一区二区三区
|