【问题标题】:How to concat a number of arrays as a single array in C#?如何在 C# 中将多个数组连接为单个数组?
【发布时间】:2019-02-22 09:01:26
【问题描述】:

如何在 C# 中将多个数组连接为单个数组?

using System;
using System.Linq;

namespace Exp2
{
    class Program2
    {
        static void Main(string[] args)
        {
            Program2 pgm = new Program2();
            Console.Write("Enter number of Arrays do you want : ");
            int numberOfArrays = int.Parse(Console.ReadLine());
            int[] narray = new int[numberOfArrays];
            int[] el=new int[100];
            int[] el1 = new int[100];
            for (int i = 0; i < narray.Length; i++)
            {
                Console.Write("Enter number of Elements do you want in Array {0}: ",i+1);
                int ai = int.Parse(Console.ReadLine());
                for (int j = 0; j < ai; j++)
                {
                    Console.Write("Enter the {1} Elements do you want in Array {0}: ", i+1,j+1);
                    el[j]= int.Parse(Console.ReadLine());
                }
                el1 =el1.Concat(el).ToArray();
            }
            foreach (int val in el1)
            {
                Console.Write(val+" ");
            }
            Console.ReadLine();

        }
    }
}

输入:

Enter number of Arrays do you want : 3
Enter number of Elements do you want in Array 1: 3
Enter the 1 Elements do you want in Array 1: 5
Enter the 2 Elements do you want in Array 1: 6
Enter the 3 Elements do you want in Array 1: 9
Enter number of Elements do you want in Array 2: 4
Enter the 1 Elements do you want in Array 2: 5
Enter the 2 Elements do you want in Array 2: 8
Enter the 3 Elements do you want in Array 2: 2
Enter the 4 Elements do you want in Array 2: 2
Enter number of Elements do you want in Array 3: 5
Enter the 1 Elements do you want in Array 3: 32
Enter the 2 Elements do you want in Array 3: 4
Enter the 3 Elements do you want in Array 3: 6
Enter the 4 Elements do you want in Array 3: 6
Enter the 5 Elements do you want in Array 3: 4

输出:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 4 6 6 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

如何只显示用户的输入?

【问题讨论】:

  • 基本上你只有一个 Int 数组而不是多个数组
  • 您可以使用List&lt;int&gt; 代替int[] 并动态创建/填充它们

标签: c# arrays list


【解决方案1】:

如果不知道元素个数,可以用List代替数组。

Concat 在列表中很容易。

List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
// Do your action to assign elements in list1 and list2

// To add list1 into list2
list2.AddRange(list1);

【讨论】:

  • 但使用此解决方案您无法区分子列表元素。
【解决方案2】:

您可以使用字典和列表:

字典将列表映射到其编号。

var dict = new Dictionary<int, List<int>>();

Console.WriteLine("Enter number of Arrays do you want : ");

int numberOfArrays = int.Parse(Console.ReadLine());

for(int k = 1; k < numberOfArrays; k++)
{
    var currentArray = dict[k] = new List<int>();

    Console.WriteLine($"Enter array {k} Length:");

    int arrayLength = int.Parse(Console.ReadLine());

    for (int j = 0; j < arrayLength; j++)
    {
       Console.WriteLine($"Enter the {j}-th Element you want in Array {k}: ");
       currentArray.Add(int.Parse(Console.ReadLine()));
    }

}

foreach(var pair in dict)
{
     Console.WriteLine($"{pair.Key}-th array elements : ");

     foreach(var element in pair.Value)
     {
        Console.WriteLine(element);
     }
}

【讨论】:

    【解决方案3】:

    现在可以使用了,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Exp2
    {
        class Program2
        {
            static void Main(string[] args)
            {
                Program2 pgm = new Program2();
                Console.Write("Enter number of Arrays do you want : ");
                int numberOfArrays = int.Parse(Console.ReadLine());
                int[] narray = new int[numberOfArrays];
                int[] el=new int[100];
                int[] el1 = new int[100];
                List<int> list1 = new List<int>(100);
                List<int> list2 = new List<int>(100);
                for (int i = 0; i < narray.Length; i++)
                {
                    Console.Write("Enter number of Elements do you want in Array {0}: ",i+1);
                    int ai = int.Parse(Console.ReadLine());
                    for (int j = 0; j < ai; j++)
                    {
                        Console.Write("Enter the {1} Elements do you want in Array {0}: ", i+1,j+1);
                        //list1[j]= int.Parse(Console.ReadLine());
                        int kj = int.Parse(Console.ReadLine());
                        list1.Add(kj);
                    }
                    //el1 =el1.Concat(el).ToArray();
                    list2.AddRange(list1);
                }
                foreach (int val in list2)
                {
                    Console.Write(val+" ");
                }
                Console.ReadLine();
    
            }
        }
    }
    

    谢谢大家..!

    【讨论】:

      【解决方案4】:

      不要像这样使用“foreach”循环使用“for”循环。

      for (int i= 0;i< el1.Length;i++)
      {   
         if (el1[i] != 0)
         Console.Write(el1[i] + " ");
         else continue;
      }
      

      警告:在某些输入情况下,您可能会得到额外的数字作为输出的最后一个元素

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2010-12-14
        • 1970-01-01
        • 2020-06-01
        • 2015-09-28
        相关资源
        最近更新 更多