数组、List、字典相互转换

    Student[] studentArray = new Student[3];
    private List<Student> studentsList = new List<Student>();
    private Student[] studentArray2 = new Student[3];
    private Dictionary<int, Student> studentDictionary = new Dictionary<int, Student>();
    private Student[] DicToArray = new Student[3];
    private Dictionary<int, Student> studentDictionary2 = new Dictionary<int, Student>();

    private List<Student> studentsList2 = new List<Student>();

  void Start()
    {
        ArrayTest();
        ArrayToList();
        ListToArray();
        ArrayToDic();
        DicToArrayFun();
        ListToDicFun();
        DicToListFun();
    }//Start ()_end


 void ArrayTest()
    {
        studentArray[0] = new Student()
        {
            Id = 0,
            Name = "z",
            Gender = "n",
        };
        studentArray[1] = new Student()
        {
            Id = 1,
            Name = "zh",
            Gender = "n",
        };
        studentArray[2] = new Student()
        {
            Id = 2,
            Name = "zht",
            Gender = "n",
        };
        for (int i = 0; i < studentArray.Length; i++)
        {
            Debug.Log("studentArray" + studentArray[i].Id + studentArray[i].Name + studentArray[i].Gender);
        }
    }

 void ArrayToList()
    {
        studentsList = studentArray.ToList<Student>();
        for (int i = 0; i < studentsList.Count; i++)
        {
            Debug.Log("studentList" + studentsList[i].Id + studentsList[i].Name + studentsList[i].Gender);
        }
    }
    void ListToArray()
    {
        studentArray2 = studentsList.ToArray<Student>();
        for (int i = 0; i < studentArray2.Length; i++)
        {
            Debug.Log("studentArray2" + studentArray2[i].Id + studentArray2[i].Name + studentArray2[i].Gender);
        }
    }

  void ArrayToDic()
    {
        studentDictionary = studentArray2.ToDictionary(key => key.Id, studentObj => studentObj);
        //foreach (var student in studentDictionary)
        //{
        //    Debug.Log("studentDictionary" +student.Key +student.Value .Id +student.Value.Name +student.Value.Gender );
        //}
        for (int i = 0; i < studentDictionary.Count ; i++)
        {
            Debug.Log(studentDictionary[i].Id + studentDictionary[i].Name + studentDictionary[i].Gender );
        }
    }


    void DicToArrayFun()
    {
        DicToArray = studentDictionary.Values.ToArray();
        for (int i = 0; i < DicToArray.Length ; i++)
        {
           Debug.Log("DicToArray" + DicToArray[i].Id + DicToArray[i].Name + DicToArray[i].Gender ); 
        }
    }


    void ListToDicFun()
    {
        studentDictionary2 = studentsList.ToDictionary(key => key.Id, studentObj => studentObj);
        for (int i = 0; i < studentDictionary2.Count; i++)
        {
            Debug.Log("listtoDic"+studentDictionary2[i].Id + studentDictionary2[i].Name +               studentDictionary2[i].Gender);
        }
    }

    void DicToListFun()
    {
        studentsList2 = studentDictionary2.Values.ToList();
        for (int i = 0; i < studentsList2.Count; i++)
        {
            Debug.Log("studentList2" + studentsList2[i].Id + studentsList2[i].Name + studentsList2[i].Gender);
        }
    }


class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值