如何将ArrayList转换为List< int>在C#中 [英] How to Convert ArrayList to List<int> in C#

查看:70
本文介绍了如何将ArrayList转换为List< int>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





  for  int  j =  0 ; j< dt.Rows.Count; j ++)
{
al.Add( Convert.ToInt16(dt.Rows [j] .ToString()。Substring( 3 ,dt.Rows [j] .ToString()。Length-3))) ;
}
List< int> list = al.Cast< int>()。ToList();





转换时会出现施法错误。

解决方案

 List< int> list = arrayList.Cast< int>()。ToList(); 



如果数组中有非整数但仍想要整数列表然后尝试使用

 List< int> integers = arrayList.OfType< int>()。ToList(); 



或您直接将项目列为整数列表,如下所示

< pre lang =c#> List< int> list = new List< int>();
for int j = 0 ; j< dt.Rows.Count; j ++)
{
list .Add(Convert.ToInt16(dt.Rows [j] .ToString()。Substring( 3 ,dt.Rows [J]的ToString()长度-3)));
}


Ref。

http://www.dotnetperls.com/convert-arraylist-list [ ^ ]

http://stackoverflow.com/questions/4437118/what-is-the-best-way-to-cast-from-arraylist-to-list [ ^ ]


ref:www.dotnetperls.com/convert-arraylist-list


Hi

for(int j=0;j<dt.Rows.Count;j++)
            {
                al.Add(Convert.ToInt16(dt.Rows[j].ToString().Substring(3,dt.Rows[j].ToString().Length-3)));
            }
 List<int> list = al.Cast<int>().ToList();



when convert it gives casting error.

解决方案

List<int> list = arrayList.Cast<int>().ToList();


if you have non integers in the array but still want integer list then try with

List<int> integers = arrayList.OfType<int>().ToList();


or you items directly to a List of integers as below

 List<int> list = new List<int>();
for(int j=0;j<dt.Rows.Count;j++)
            {
                list .Add(Convert.ToInt16(dt.Rows[j].ToString().Substring(3,dt.Rows[j].ToString().Length-3)));
            }


Ref.
http://www.dotnetperls.com/convert-arraylist-list[^]
http://stackoverflow.com/questions/4437118/what-is-the-best-way-to-cast-from-arraylist-to-list[^]


ref : www.dotnetperls.com/convert-arraylist-list


这篇关于如何将ArrayList转换为List&lt; int&gt;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆