zoukankan      html  css  js  c++  java
  • C#中ArrayList 、Array与、string、string[]数组的相关转换

    一、ArrayList stringstring[]数组的转换

           1.ArrayList 转换为 string[] :       

     ArrayList list = new ArrayList();
      list.Add("aaa");
      list.Add("bbb");
      //转换成数组
      string[] arrString = (string[])list.ToArray(typeof( string)) ;
    

      2、string[] 转换为 ArrarList :

    ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });

           3、ArrayList 转换为 string :

      ArrayList list = new ArrayList();
      list.Add("aaa");
      list.Add("bbb"); 
      //转换成数组
    String str=string.Join(",",(string[])list.ToArray(typeof( string)));
    

        4、string 转换为 ArrayList :

      string str="1,2,3,4,5";
      ArrayList b = new ArrayList( str.Split(',') ) ;
    

      

    二、ArrayList和Array相互之间的转化

        

    以下是把ArrayList数组中的值拷贝到Array中去的实例用法

       //int[] IResult=new int[al.Count];
       //al.CopyTo(IResult);
       //或是用下面的方法
     int[] IResult = (int[])al.ToArray(typeof(Int32));//ToArray(Int32);这样错误,一定要强制类型转换
      //Person[] personArrayFromList = (Person[])personList.ToArray(typeof(Person));  
             foreach(int item   in IResult)
                {
                  Response.Write(item.ToString());
                }
    Response.Write("以下是把Array数组中的值拷贝到ArrayList中去的实例用法" + "<br>" + "结果为:<br>");
                int[] a ={ 222, 333, 555 };
                ArrayList arrList = new ArrayList();
                foreach (object obj in a)//或foreach (int obj in a)
                {
                    arrList.Add(obj);
                    Response.Write(obj+"<br>");
                }
    

      

  • 相关阅读:
    Codeforces 758D:Ability To Convert(思维+模拟)
    Codeforces 758B:Blown Garland(模拟)
    BZOJ-1053 反素数
    BZOJ-1024 生日快乐
    BZOJ-1036 树的统计
    BZOJ-1029 建筑抢修
    BZOJ-1059 矩阵游戏
    BZOJ-1026 windy数
    BZOJ-1019 汉诺塔
    BZOJ-1031 字符加密
  • 原文地址:https://www.cnblogs.com/BObwei/p/4890524.html
Copyright © 2011-2022 走看看