会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Hello Caviare!
博客园
首页
新随笔
新文章
联系
订阅
管理
迭代器和排序基本使用
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
testApp
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
usingIteration();
usingComparer();
}
/**/
///
<summary>
///
迭代器使用Demo
///
</summary>
void
usingIteration()
{
System.Text.StringBuilder sbShow
=
new
StringBuilder();
System.Collections.IEnumerator ie
=
this
.comboBox1.Items.GetEnumerator();
while
(ie.MoveNext())
{
sbShow.AppendLine(ie.Current.ToString());
}
MessageBox.Show(sbShow.ToString());
/**/
/*
显
---line1---
---line5---
---line3---
---line2---
---line4---
---line7---
---line8---
---line6---
*/
}
/**/
///
<summary>
///
LIST排序Demo
///
</summary>
void
usingComparer()
{
StringSort sort
=
new
StringSort();
System.Collections.ArrayList arrList
=
new
System.Collections.ArrayList(comboBox1.Items);
arrList.Sort(sort);
System.Text.StringBuilder sbShow
=
new
StringBuilder();
for
(
int
i
=
0
; i
<
arrList.Count; i
++
)
{
sbShow.AppendLine(arrList[i].ToString());
}
MessageBox.Show(sbShow.ToString());
/**/
/*
---line1---
---line2---
---line3---
---line4---
---line5---
---line6---
---line7---
---line8---
*/
}
}
public
class
StringSort : System.Collections.IComparer
{
public
int
Compare(
object
x,
object
y)
{
string
str1
=
x
as
string
;
string
str2
=
y
as
string
;
if
(str1
==
null
||
str2
==
null
)
{
throw
new
Exception(
"
Item Error!
"
);
}
else
{
if
(intGetIndex(str1)
<
intGetIndex(str2))
{
return
-
1
;
}
else
{
return
0
;
}
}
}
int
intGetIndex(
string
str)
{
return
Convert.ToInt32(str.Substring(
7
,
1
));
//
---line1---
}
}
}
posted on
2007-11-02 17:31
Caviare
阅读(
893
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部