代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

将数组的一部分复制到另一个数组中[重复]

  •  3
  • leora Matt Lacey  · 技术社区  · 15 年前

    可能重复:
    How to copy part of an array to another array in C#?

    如果我有:

    string[] myArray =  . . . .
    

    它是一个长度为10的数组。如何创建一个新的字符串数组,它是第一个数组的第2到第10个元素,而不循环?

    3 回复  |  直到 15 年前
        1
  •  9
  •   codekaizen    15 年前

    使用 System.Array.Copy :

    string[] myArray = ....
    string[] copy = new string[10];
    Array.Copy(myArray, 2, copy, 0, 10);    
    
        2
  •  0
  •   Mitch Wheat    15 年前

    Array.Copy

    (但请注意,它将在封面下循环,这是最理想的方式)。

    Example .

        3
  •  -1
  •   Joe Garrett    15 年前
       // Copies the last two elements from the Object array to the Int32 array.
       Array::Copy( myObjArray, myObjArray->GetUpperBound(0) - 1, myIntArray, myIntArray->GetUpperBound(0) - 1, 
    

    看: http://msdn.microsoft.com/en-us/library/system.array.copy%28VS.71%29.aspx

    array.copy(myintarray,myintarray.getlowerbound(0),myobjarray,myobjarray.getlowerbound(0),1);