簡體   English   中英

System.Array 到 List 的轉換

[英]Conversion of System.Array to List

昨晚我夢見以下事情是不可能的。 但在同一個夢里,來自 SO 的人卻告訴了我不同的說法。 因此我想知道是否可以將System.Array轉換為List

Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);
ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);

List<int> lst = ints.OfType<int>(); // not working

給自己省點痛...

using System.Linq;

int[] ints = new [] { 10, 20, 10, 34, 113 };

List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.

也只能...

List<int> lst = new List<int> { 10, 20, 10, 34, 113 };

或者...

List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);

或者...

List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });

或者...

var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });

List 還有一個構造函數重載可以工作......但我想這需要一個強類型數組。

//public List(IEnumerable<T> collection)
var intArray = new[] { 1, 2, 3, 4, 5 };
var list = new List<int>(intArray);

... 對於數組類

var intArray = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
    intArray.SetValue(i, i);
var list = new List<int>((int[])intArray);

有趣的是,沒有人回答這個問題,OP 沒有使用強類型int[]而是使用Array

您必須將Array轉換為它的實際情況,一個int[] ,然后您可以使用ToList

List<int> intList = ((int[])ints).ToList();

請注意, Enumerable.ToList調用列表構造函數,該構造函數首先檢查參數是否可以強制轉換為ICollection<T> (數組實現),然后它將使用更有效的ICollection<T>.CopyTo方法而不是枚舉序列。

最簡單的方法是:

int[] ints = new [] { 10, 20, 10, 34, 113 };

List<int> lst = ints.ToList();

或者

List<int> lst = new List<int>();
lst.AddRange(ints);

如果您想將枚舉數組作為列表返回,您可以執行以下操作。

using System.Linq;

public List<DayOfWeek> DaysOfWeek
{
  get
  {
    return Enum.GetValues(typeof(DayOfWeek))
               .OfType<DayOfWeek>()
               .ToList();
  }
}

你基本上可以這樣做:

int[] ints = new[] { 10, 20, 10, 34, 113 };

這是你的數組,你可以像這樣調用你的新列表:

 var newList = new List<int>(ints);

您也可以對復雜對象執行此操作。

在 vb.net 中就這樣做

mylist.addrange(intsArray)

或者

Dim mylist As New List(Of Integer)(intsArray)

你可以試試你的代碼:

Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);

ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);

int[] anyVariable=(int[])ints;

然后你可以使用 anyVariable 作為你的代碼。

我知道兩種方法:

List<int> myList1 = new List<int>(myArray);

或者,

List<int> myList2 = myArray.ToList();

我假設您了解數據類型,並且會根據需要更改類型。

只需使用現有方法.. .ToList();

   List<int> listArray = array.ToList();

親吻(保持簡單,先生)

我希望這是有幫助的。

enum TESTENUM
    {
        T1 = 0,
        T2 = 1,
        T3 = 2,
        T4 = 3
    }

獲取字符串值

string enumValueString = "T1";

        List<string> stringValueList =  typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m => 
            Convert.ToString(m)
            ).ToList();

        if(!stringValueList.Exists(m => m == enumValueString))
        {
            throw new Exception("cannot find type");
        }

        TESTENUM testEnumValueConvertString;
        Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertString);

獲取整數值

        int enumValueInt = 1;

        List<int> enumValueIntList =  typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m =>
            Convert.ToInt32(m)
            ).ToList();

        if(!enumValueIntList.Exists(m => m == enumValueInt))
        {
            throw new Exception("cannot find type");
        }

        TESTENUM testEnumValueConvertInt;
        Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertInt);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM