Array subset copying

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derrick

    Array subset copying

    If I have an array..

    object[] myObjs = someMethod.Sear chForObjs();

    and the length of that array is 100, but I want to extract the last 80
    objects into another array, what's the cleanest way to do that?
    Array.CopyTo seems to allow specification of target index to start copying
    into, but not the source array index. Do I need to brute force it and go
    with a for loop?

    Thanks in advance!

    Derrick


  • Kurt

    #2
    RE: Array subset copying

    Derrick the Array.Copy function should work for you.

    [C#]
    public static void Copy(Array sourceArray, int sourceIndex, Array
    destinationArra y,
    int destinationInde x, int length);

    "Derrick" wrote:
    [color=blue]
    > If I have an array..
    >
    > object[] myObjs = someMethod.Sear chForObjs();
    >
    > and the length of that array is 100, but I want to extract the last 80
    > objects into another array, what's the cleanest way to do that?
    > Array.CopyTo seems to allow specification of target index to start copying
    > into, but not the source array index. Do I need to brute force it and go
    > with a for loop?
    >
    > Thanks in advance!
    >
    > Derrick
    >
    >
    >[/color]

    Comment

    • Peter Bromberg [C# MVP]

      #3
      Re: Array subset copying

      Take a look at the System.Buffer.B lockCopy method.
      Peter

      "Derrick" <derrick1298@ex cite.com> wrote in message
      news:uvOA2iUMFH A.2252@TK2MSFTN GP15.phx.gbl...[color=blue]
      > If I have an array..
      >
      > object[] myObjs = someMethod.Sear chForObjs();
      >
      > and the length of that array is 100, but I want to extract the last 80
      > objects into another array, what's the cleanest way to do that?
      > Array.CopyTo seems to allow specification of target index to start copying
      > into, but not the source array index. Do I need to brute force it and go
      > with a for loop?
      >
      > Thanks in advance!
      >
      > Derrick
      >
      >[/color]


      Comment

      • Peter Bromberg [C# MVP]

        #4
        Re: Array subset copying

        Take a look at the System.Buffer.B lockCopy method.
        Peter

        "Derrick" <derrick1298@ex cite.com> wrote in message
        news:uvOA2iUMFH A.2252@TK2MSFTN GP15.phx.gbl...[color=blue]
        > If I have an array..
        >
        > object[] myObjs = someMethod.Sear chForObjs();
        >
        > and the length of that array is 100, but I want to extract the last 80
        > objects into another array, what's the cleanest way to do that?
        > Array.CopyTo seems to allow specification of target index to start copying
        > into, but not the source array index. Do I need to brute force it and go
        > with a for loop?
        >
        > Thanks in advance!
        >
        > Derrick
        >
        >[/color]


        Comment

        Working...