简体   繁体   English

在 C# 中用较小的数组复制/填充大数组的最佳方法是什么?

[英]What's the best way to copy/fill a large array with a smaller array in C#?

I have a large int[] array and a much smaller int[] array.我有一个大的 int[] 数组和一个小得多的 int[] 数组。 I want to fill up the large array with values from the small array, by repeat copying the small array into the large array until it is full (so that large[0] = large[13] = large[26]... = small[0] etc.).我想用小数组中的值填充大数组,方法是重复将小数组复制到大数组中直到它已满(这样 large[0] = large[13] = large[26]... =小 [0] 等)。 I already have a simple method:我已经有一个简单的方法:

int iSource = 0;
for (int i = 0; i < destArray.Length; i++)
{
    if (iSource >= sourceArray.Length)
    {
        iSource = 0; // reset if at end of source
    }
    destArray[i] = sourceArray[iSource++];
}

But I need something more elegant, and hopefully faster.但我需要更优雅的东西,希望更快。

Have your loop work using the Array.Copy() overload that lets you copy from one array into the a particular index in the destination array. 使用Array.Copy()重载让循环工作,该重载使您可以从一个数组复制到目标数组中的特定索引。

if (sourceArray.Length == 0) return; // don't get caught in infinite loop

int idx = 0;

while ((idx + sourceArray.Length) < destArray.Length) {
    Array.Copy( sourceArray, 0, destArray, idx, sourceArray.Length);

    idx += sourceArray.Length;
}

Array.Copy( sourceArray, 0, destArray, idx, destArray.Length - idx);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Temp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
            int[] array2 = new int[213];

            for (int i = 0; i < array2.Length; i += array.Length)
            {
                int length = array.Length;
                if ((i + array.Length) >= array2.Length)
                    length = array2.Length - i;
                Array.Copy(array, 0, array2, i, length);
            }

            int count = 0;
            foreach (int i in array2)
            {
                Console.Write(i.ToString() + " " + (count++).ToString() + "\n");
            }

            Console.Read();
        }
    }
}

:) :)

EDIT Found bug where if they were not dividable by each other it would crash. 编辑发现错误,如果它们不能彼此分开,将会崩溃。 Fixed now :) 现在已修复:)

Interestingly the winning answer is the slowest with the provided source array! 有趣的是,在提供的源阵列中,最成功的答案是最慢的!

The solution I was going to propose was 我要提出的解决方案是

for (int i = 0; i < destArray.Length; i++)
{
    destArray[i] = sourceArray[i%sourceArray.Length];
}

but when i tested the perf over 100000 iterations using the inputs in the answering question it performed worse than the questioners loop. 但是当我使用回答问题中的输入测试性能超过100000次迭代时,它的表现比问者循环差。

here is the output from my little test app 这是我的小测试应用程序的输出

array copy 164ms      (Nelson LaQuet's code) 
assign copy 77ms      (MusiGenesis code)
assign mod copy 161ms (headsling's code)
for (int i=0;source.Length!= 0 && source.Length!= i;i++)
        {
            destination[i] = source[i];
        }

i got this from my old project and modified it.我从我的旧项目中得到这个并修改了它。 u may want to change a thing or two typos in it given there might be typos in it你可能想更改其中的一两个错别字,因为其中可能有错别字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM