[英]How to convert ArrayList into string array(string[]) in c#
如何在C#中将ArrayList
转换为string[]
?
string[] myArray = (string[])myarrayList.ToArray(typeof(string));
A simple Google or search on MSDN would have done it. 一个简单的谷歌或MSDN上的搜索就可以做到。 Here:
这里:
ArrayList myAL = new ArrayList();
// Add stuff to the ArrayList.
String[] myArr = (String[]) myAL.ToArray( typeof( string ) );
using System.Linq; public static string[] Convert(this ArrayList items) { return items == null ? null : items.Cast<object>() .Select(x => x == null ? null : x.ToString()) .ToArray(); }
string[] stringArray = (string[])arrayList.ToArray(typeof(string));
Try do that with ToArray()
method. 尝试使用
ToArray()
方法。
ArrayList a= new ArrayList(); //your ArrayList object
var array=(String[])a.ToArray(typeof(string)); // your array!!!
Another way is as follows. 另一种方式如下。
System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add("1");
al.Add("2");
al.Add("3");
string[] asArr = new string[al.Count];
al.CopyTo(asArr);
You can use CopyTo method of ArrayList object. 您可以使用ArrayList对象的CopyTo方法。
Let's say that we have an arraylist, which has String Type as Elements. 假设我们有一个arraylist,它有String Type作为Elements。
strArrayList.CopyTo(strArray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.