Dict.CN 在线词典, 英语学习, 在线翻译
Robot·H【移动鼠标,点击生活】
博客园
首页
新随笔
订阅
管理
随笔-179 评论-145 文章-1 trackbacks-1
Converting ArrayList to Array / Array to ArrayList C#
This article explains the easiest way to convert Array
object
into ArrayList and the reverse. Author: Aldwin Enriquez
aldwin.net
Posted Date:
06
Dec,
2005
.NET Classes used :
System.Collections.ArrayList
Introduction
Manipulating arrays
is
one among the most common task
in
an application development. There were times you need to use array of objects to use the power of
object
properties and there were times you might like to use an ArrayList
for
flexibility. Sometimes switching back and forth between these two objects becomes a royal pain
in
the neck. This article leads you the way on how to
do
things better
The hard way
Mostly beginners
do
this
conversion manually. In the
case
of converting from
object
array into an ArrayList, one might instantiate a
new
ArrayList then iterates through each
object
in
the array and add it to the ArrayList.
Lets assume we have an
object
called Person. Typically here
is
what
is
commonly done:
Person[] personArray
=
myPerson.GetPersons();
ArrayList personList
=
new
ArrayList();
foreach
(Person objPerson
in
personArray)
{
personList.Add(objPerson);
}
And creating an
object
array from an ArrayList might be coded
this
way
Person[] personArrayFromList
=
new
Person[personList.Count];
int
arrayCounter
=
0
;
foreach
(Person objPerson
in
personList)
{
personArrayFromList.SetValue(objPerson,arrayCounter
++
);
}
The easy one
But why
do
it that way
while
we can just use built
-
in
methods within the .NET classes.
To perform conversions from
object
array to an ArrayList use the ArrayList.Adapter method. This method takes an IList to be wrapped into an ArrayList. Now the procedure above can be coded like
this
:
Person[] personArray
=
myPerson.GetPersons();
ArrayList personList
=
ArrayList.Adapter(personArray);
To perform conversions from ArrayList to
object
array use the ArrayList.ToArray method. Now the procedure above can be coded like
this
:
Person[] personArrayFromList
=
(Person[])personList.ToArray(
typeof
(Person));
Don’t forget the casting preceding the arraylist.ToArray method, otherwise you’ll
get
an error message at compile time saying you can’t convert that arraylist into Person array.
Summary
So next time you convert an
object
array to arraylist use the
static
ArrayList.Adapter method and doing the reverse use the ToArray method of the arraylist
object
.
posted on 2007-09-29 16:58
Robot·H
阅读(218)
评论(0)
编辑
收藏
所属分类:
C#2.0
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
另存
打印
所属分类的其他文章:
·
您是如何读取模板的?
·
大家讨论一下这个图如何设计程序。
·
Petshop的一个疑惑
·
如何把string解析为int?[C#]
·
判断字符串为空的最有效的方法。
·
StategyPattern
·
如何实现foreach
·
params(C# 参考)
·
面试1
·
完善了一下一个哥们的面试题
最新IT新闻:
·
奥运核心资源被分食 搜狐央视网谁忽悠谁?
·
微软推新型搜索技术"BrowseRank"挑战谷歌
·
2008年7月26日IT博客精选
·
微软每年向Apache捐10万美元支持开源软件
·
AOL将关闭3个网站以降低成本 集中发力广告
博客园新闻频道
博客园首页
社区
一辈子用心做好一件事
我的最新闪存
昨天梦见自己把自己像剥鱼一样给剥了。好恐怖!自己手里拿着菜刀先剃掉自己的头发。呜呜~。。~
7-4 08:46
<
2007年9月
>
日
一
二
三
四
五
六
26
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
30
1
2
3
4
5
6
与我联系
发短消息
留言簿
给我留言
查看留言
我管理的小组
乱七八圾
我参加的小组
ASP.NET
随笔分类
.NetFramework(6)
Ajax(1)
Ajax
asp.Net2.0(47)
C#2.0(26)
CSS+Javacript(18)
EC(4)
English(1)
Interview(2)
Office Forms Server+SharePoint Server(1)
ProgramLife(20)
Sqlserver(15)
TechBase(10)
UML(3)
XML
每日一句英语(1)
随笔档案
2008年7月 (23)
2008年6月 (26)
2008年5月 (24)
2008年4月 (11)
2008年3月 (8)
2008年1月 (2)
2007年12月 (2)
2007年11月 (5)
2007年10月 (1)
2007年9月 (26)
2007年8月 (21)
2007年7月 (13)
2007年6月 (11)
2007年5月 (3)
2007年4月 (3)
Asp.Net
CodeProject
SilverLight
UML
w3schools
w3school中文
博客堂
说故事、学模式
职场生涯
Company
facebook
康盛创想
DB
SqlServer Magazine
SqlServerCenter
EC
艾瑞
创意纪
电子商务博客
English
bjEnglish
China Daily
EnglishTown
i词霸
language_tools
NBC
华尔街英语
JS+CSS+XML+DOM
Ajax基础教程 - 免费试读 - book.csdn.net
最新随笔
1. facebook
2. 贝塔斯曼唇亡,当当卓越齿寒
3. 电子商务网站
4. SNS 网站
5. [转帖]淘宝开放平台Taobao Open Platform的机会
6. 理财
7. Enterprise Library系列文章回顾与总结
8. Data Access Application block
9. 端口
10. 写技术文档的朋友来着看看
11. 小R每日一句英语
12. DOM:element.addEventListener
13. 35句话,不看你后悔!
14. 你触发了谁?
15. Music_北京欢迎您
16. [转]新三十年目睹之怪现状
17. 字符换行
18. 您是如何读取模板的?
19. 大家讨论一下这个图如何设计程序。
20. [转帖].Net pet shop 4 和 MSMQ
搜索
积分与排名
积分 - 23281
排名 - 1662
最新评论
1. re: 贝塔斯曼唇亡,当当卓越齿寒
你把古汉语理解错啦 贝塔斯曼和当当没有直接的联系 所以不能用唇亡齿寒来形容
--yangyanyang
2. re: 贝塔斯曼唇亡,当当卓越齿寒
瞎扯淡,贝塔斯曼和当当有屁的关系
--偶卖糕的
3. re: 电子商务网站
@丁学 现在电子商务很火热 csdn的强力推出sns类的"校内网". 电子商务和sns能不能 来一个联姻还是未知数。 ...
--Robot·H
4. re: 电子商务网站
@丁学
改过。
不知道现在还有什么知名的垂直电子商务。
知道的贴上去来。
--Robot·H
5. re: 电子商务网站
淘宝的主域名应该是
--丁学
阅读排行榜
1. 【已经找到】准备找工作了[北京地区C#](697)
2. 大家讨论一下这个图如何设计程序。(520)
3. 妓女也玩OCP(414)
4. PetShop介绍集锦(403)
5. 完美的Div弹出窗口(325)
评论排行榜
1. 大家讨论一下这个图如何设计程序。(18)
2. 面试1(11)
3. 真的很无奈(10)
4. 完美的Div弹出窗口(9)
5. 【已经找到】准备找工作了[北京地区C#](9)
60天内阅读排行
1. 大家讨论一下这个图如何设计程序。(520)
2. 完美的Div弹出窗口(325)
3. js值得收藏(243)
4. document.getElementById为空或不是对象的解决方法 (220)
5. JavaScript replace() 方法(199)
Msn:glory_yimart@hotmail.com QQ:2839849 Emal:yimart at 163.com