在特定索引c#下将一个数组复制到另一个数组 [英] copy one array to another at certain index c#

查看:66
本文介绍了在特定索引c#下将一个数组复制到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用for循环构建此复制方法,到目前为止,我已将复制array1下面的代码写到array2

I need to build this copy method using for loops, so far I wrote the code below that copy array1 to array2

public static void CopyTo1(int[] array1, int[] array2, int startat)
    {
        for (int i = 0; i < array1.Length; i++)
        {
           array1[i] = array2[i];
           Console.Write(array2[i].ToString());
        }
    }

我如何使 startat确定在哪个索引中复制将开始吗?

how can i make "startat" determine in what index the copying shall start?

int [] array1 = new int [3] {4, 5, 6};
int [] array2 = new int [6] {1, 2, 3, 0, 0, 0};
startat=3;
// array2 = [1, 2, 3, 4, 5, 6]


推荐答案

public static void CopyTo1(int[] array1, int[] array2, int startat)
    {
        for (int i = 0; i < array1.Length; i++)
        {
           array2[startat] = array1[i];
            startat++;
           Console.Write(array2[i].ToString());
        }
    }

这篇关于在特定索引c#下将一个数组复制到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆