📌  相关文章
📜  如何在C#中将整数数组转换为列表?

📅  最后修改于: 2021-05-30 00:42:03             🧑  作者: Mango

我们给一个整数数组ARR和任务是整型数组转换成C#的列表LST。为了执行此任务,我们有以下方法:

方法1: List class 表示可以通过索引访问的对象列表。它位于System.Collection.Generic命名空间下。 List 类还提供了搜索,排序和操作列表的方法。这也用于将给定的整数数组转换为列表。

句法:

List lst = new List { 1, 2, 3};

例子:

C#
// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
    
    static void Main(string[] args)
    {
        // given integer array 
        // { 10, 20, 30, 40, 50 }
          
        // using List class
        List lst = new List { 10, 20, 30, 40, 50 };
        
          // you can write the above line of code as
          // int[] ints = new [] { 10, 20, 30, 40, 50 };
        // List lst = ints.OfType().ToList();
        
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
    }
}


C#
// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // List(IEnumerable) 
        // Constructor
        // is a used to convert a 
        // given an integer array 
        // to the list
      
        List L = new List (arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
    }
}


C#
// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // AddRange(IEnumerable) 
        // Method is a used to convert
        // given an integer array 
        // to the list
      
        List L = new List();
        L.AddRange(arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
           foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
  
    }
}


C#
// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // ToList() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List L = arr.ToList();
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
          foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}


C#
// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // Add() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List L = new List();
          
        for(int i = 0 ; i < arr.Length ; i++)
        {
            L.Add(arr[i]);
        }
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}


输出:

10 20 30 40 50

方法2: List (IEnumerable )构造函数:使用此构造函数,可以初始化List 的新实例,该实例包含从指定集合中复制的元素,并具有足够的容量来容纳复制的元素数量。因此,这也可以用于将给定的整数数组转换为列表。

句法:

List lst = new List(integer_array);

例子:

C#

// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // List(IEnumerable) 
        // Constructor
        // is a used to convert a 
        // given an integer array 
        // to the list
      
        List L = new List (arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
    }
}

输出:

10 20 30 40 50

方法3: AddRange(IEnumerable )方法:此方法 用于将指定集合的元素添加到List 的末尾。因此,这也可以用于将给定的整数数组转换为列表。

句法:

List lst = new List();
lst.AddRange(integer_array)

例子:

C#

// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // AddRange(IEnumerable) 
        // Method is a used to convert
        // given an integer array 
        // to the list
      
        List L = new List();
        L.AddRange(arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
           foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
  
    }
}

输出:

10 20 30 40 50

方法4:ToList()方法: Enumerate.Tolist方法来自System.Linq命名空间,该命名空间从IEnumerable 创建List 它返回一个List ,其中包含来自输入序列的元素。

句法:

List lst = integer_array.ToList();

例子:

C#

// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // ToList() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List L = arr.ToList();
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
          foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}

输出:

10 20 30 40 50

方法5: Add()方法 此方法用于将对象添加到列表的末尾。因此,这也可以用于将给定的整数数组转换为列表。

句法:

List lst = new List();
lst.Add(int val);

例子:

C#

// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List get_list(int[] arr) 
    {
        // Add() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List L = new List();
          
        for(int i = 0 ; i < arr.Length ; i++)
        {
            L.Add(arr[i]);
        }
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}

输出:

10 20 30 40 50