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

时间:2019-02-22 09:01:27

标签: c# arrays list

如何在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

如何仅显示用户输入?

3 个答案:

答案 0 :(得分:0)

如果您不知道元素的数量,则可以使用列表而不是数组。

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);

答案 1 :(得分:0)

您可以使用字典和列表:

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

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);
     }
}

答案 2 :(得分:0)

请勿使用“ foreach”循环,而应使用“ for”循环。

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

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