Copy/Duplicate Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Copy/Duplicate Arrays

Hybrid View

  1. #1
    David Gaines Guest

    Copy/Duplicate Arrays

    Greetings,

    I need to replicate an array, but do not want the new array to reference the
    original array's memory location. In other words, I need a completely
    separate, exact copy of the original array so that I can manipulate its
    elements without impacting the original array elements.

    I have tried Array.Copy, but that seems to just point the new array's
    elements to the memory location of the original array's elements. Is there a
    way to create a clean, new copy of an existing array?

    Thanks,
    Dave Gaines

    --
    Regards,
    Dave Gaines
    dgaines@atlanticappraisal.com

    Associate Appraiser
    Atlantic Appraisal Company, Inc.
    2355 Huguenard Drive, Suite 202
    Lexington, KY 40503
    (859) 273-2060, x238



  2. #2
    Steve Cochran Guest

    Re: Copy/Duplicate Arrays

    try

    array2=3Darray1.clone

    steve

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message =
    news:3dad5c3c$1@tnews.web.devx.com...
    Greetings,

    I need to replicate an array, but do not want the new array to reference =
    the
    original array's memory location. In other words, I need a completely
    separate, exact copy of the original array so that I can manipulate its
    elements without impacting the original array elements.

    I have tried Array.Copy, but that seems to just point the new array's
    elements to the memory location of the original array's elements. Is =
    there a
    way to create a clean, new copy of an existing array?

    Thanks,
    Dave Gaines

    --
    Regards,
    Dave Gaines
    dgaines@atlanticappraisal.com

    Associate Appraiser
    Atlantic Appraisal Company, Inc.
    2355 Huguenard Drive, Suite 202
    Lexington, KY 40503
    (859) 273-2060, x238



  3. #3
    David Gaines Guest

    Re: Copy/Duplicate Arrays

    Thanks for the response Steve, but that doesn't work.

    Clone apparently also just points the array2 to the array1 memory.

    Thanks again anyway,
    Dave

    "Steve Cochran" <scochran@chattanooga.net> wrote in message
    news:3dad5cf5$1@tnews.web.devx.com...
    try

    array2=array1.clone

    steve

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    news:3dad5c3c$1@tnews.web.devx.com...
    Greetings,

    I need to replicate an array, but do not want the new array to reference the
    original array's memory location. In other words, I need a completely
    separate, exact copy of the original array so that I can manipulate its
    elements without impacting the original array elements.

    I have tried Array.Copy, but that seems to just point the new array's
    elements to the memory location of the original array's elements. Is there a
    way to create a clean, new copy of an existing array?

    Thanks,
    Dave Gaines

    --
    Regards,
    Dave Gaines
    dgaines@atlanticappraisal.com

    Associate Appraiser
    Atlantic Appraisal Company, Inc.
    2355 Huguenard Drive, Suite 202
    Lexington, KY 40503
    (859) 273-2060, x238




  4. #4
    iGadget Guest

    Re: Copy/Duplicate Arrays

    No it doesn't, it clones the array, so perhaps your problem is that you have
    an array of references, in which case you have a completely new array of
    references but they point to the same objects?
    Perhaps some more information would help, like what is it an array of?

    Cheers,
    iGadget

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    news:3dad78c0$1@tnews.web.devx.com...
    > Thanks for the response Steve, but that doesn't work.
    >
    > Clone apparently also just points the array2 to the array1 memory.
    >
    > Thanks again anyway,
    > Dave
    >
    > "Steve Cochran" <scochran@chattanooga.net> wrote in message
    > news:3dad5cf5$1@tnews.web.devx.com...
    > try
    >
    > array2=array1.clone
    >
    > steve
    >
    > "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    > news:3dad5c3c$1@tnews.web.devx.com...
    > Greetings,
    >
    > I need to replicate an array, but do not want the new array to reference

    the
    > original array's memory location. In other words, I need a completely
    > separate, exact copy of the original array so that I can manipulate its
    > elements without impacting the original array elements.
    >
    > I have tried Array.Copy, but that seems to just point the new array's
    > elements to the memory location of the original array's elements. Is there

    a
    > way to create a clean, new copy of an existing array?
    >
    > Thanks,
    > Dave Gaines
    >
    > --
    > Regards,
    > Dave Gaines
    > dgaines@atlanticappraisal.com
    >
    > Associate Appraiser
    > Atlantic Appraisal Company, Inc.
    > 2355 Huguenard Drive, Suite 202
    > Lexington, KY 40503
    > (859) 273-2060, x238
    >
    >
    >




  5. #5
    Steve Cochran Guest

    Re: Copy/Duplicate Arrays

    Well, one book I have says one thing and this says more or less another:

    http://msdn.microsoft.com/library/en...emarrayclassc=
    lonetopic.asp

    you might try CopyTo and see what that does.

    steve

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message =
    news:3dad78c0$1@tnews.web.devx.com...
    Thanks for the response Steve, but that doesn't work.

    Clone apparently also just points the array2 to the array1 memory.

    Thanks again anyway,
    Dave

    "Steve Cochran" <scochran@chattanooga.net> wrote in message
    news:3dad5cf5$1@tnews.web.devx.com...
    try

    array2=3Darray1.clone

    steve

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    news:3dad5c3c$1@tnews.web.devx.com...
    Greetings,

    I need to replicate an array, but do not want the new array to reference =
    the
    original array's memory location. In other words, I need a completely
    separate, exact copy of the original array so that I can manipulate its
    elements without impacting the original array elements.

    I have tried Array.Copy, but that seems to just point the new array's
    elements to the memory location of the original array's elements. Is =
    there a
    way to create a clean, new copy of an existing array?

    Thanks,
    Dave Gaines

    --
    Regards,
    Dave Gaines
    dgaines@atlanticappraisal.com

    Associate Appraiser
    Atlantic Appraisal Company, Inc.
    2355 Huguenard Drive, Suite 202
    Lexington, KY 40503
    (859) 273-2060, x238




  6. #6
    David Gaines Guest

    Re: Copy/Duplicate Arrays

    Thanks to all for the suggestions; here is some more information. I've declared an array of structures as shown below. I am now trying to create a clean copy of this array, also as excerpted below.

    BTW, to make it work, I have created a work around where I step through every element of my new array and just set it equal to the value in the old array. It works as it is, but I thought there might be a more "correct" method. I have excerpted relevant portions of my code below.

    Structure Individual
    Public Number As Integer
    Public Chromosone() As Integer
    Public Value As Decimal
    Public Fitness As Decimal
    Public Parent1, Parent2, XSite1, XSite2 As Integer
    End Structure

    Public GenePool() As Individual

    Dim TempPop As Individual()

    ReDim TempPop(UBound(GenePool))

    'TempPop = GenePool.Clone

    'Array.Copy(GenePool, TempPop, UBound(GenePool) + 1)

    For i = 0 To UBound(TempPop)

    ReDim TempPop(i).Chromosone(UBound(GenePool(i).Chromosone))

    For j = 0 To UBound(TempPop(i).Chromosone)

    TempPop(i).Chromosone(j) = GenePool(i).Chromosone(j)

    TempPop(i).Number = GenePool(i).Number

    Next

    Next


    "iGadget" <iGadget_@hotmail.com> wrote in message news:3dad8323$1@tnews.web.devx.com...
    > No it doesn't, it clones the array, so perhaps your problem is that you have
    > an array of references, in which case you have a completely new array of
    > references but they point to the same objects?
    > Perhaps some more information would help, like what is it an array of?
    >
    > Cheers,
    > iGadget
    >
    > "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    > news:3dad78c0$1@tnews.web.devx.com...
    > > Thanks for the response Steve, but that doesn't work.
    > >
    > > Clone apparently also just points the array2 to the array1 memory.
    > >
    > > Thanks again anyway,
    > > Dave
    > >
    > > "Steve Cochran" <scochran@chattanooga.net> wrote in message
    > > news:3dad5cf5$1@tnews.web.devx.com...
    > > try
    > >
    > > array2=array1.clone
    > >
    > > steve
    > >
    > > "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    > > news:3dad5c3c$1@tnews.web.devx.com...
    > > Greetings,
    > >
    > > I need to replicate an array, but do not want the new array to reference

    > the
    > > original array's memory location. In other words, I need a completely
    > > separate, exact copy of the original array so that I can manipulate its
    > > elements without impacting the original array elements.
    > >
    > > I have tried Array.Copy, but that seems to just point the new array's
    > > elements to the memory location of the original array's elements. Is there

    > a
    > > way to create a clean, new copy of an existing array?
    > >
    > > Thanks,
    > > Dave Gaines
    > >
    > > --
    > > Regards,
    > > Dave Gaines
    > > dgaines@atlanticappraisal.com
    > >
    > > Associate Appraiser
    > > Atlantic Appraisal Company, Inc.
    > > 2355 Huguenard Drive, Suite 202
    > > Lexington, KY 40503
    > > (859) 273-2060, x238
    > >
    > >
    > >

    >
    >


  7. #7
    JGCS Guest

    Re: Copy/Duplicate Arrays


    "David Gaines" <dgaines@atlanticappraisal.com> wrote:
    >Greetings,
    >
    >I need to replicate an array, but do not want the new array to reference

    the
    >original array's memory location. In other words, I need a completely
    >separate, exact copy of the original array so that I can manipulate its
    >elements without impacting the original array elements.
    >
    >I have tried Array.Copy, but that seems to just point the new array's
    >elements to the memory location of the original array's elements. Is there

    a
    >way to create a clean, new copy of an existing array?
    >
    >Thanks,
    >Dave Gaines
    >
    >--
    >Regards,
    >Dave Gaines
    >dgaines@atlanticappraisal.com
    >
    >Associate Appraiser
    >Atlantic Appraisal Company, Inc.
    >2355 Huguenard Drive, Suite 202
    >Lexington, KY 40503
    >(859) 273-2060, x238
    >
    >

    Hi David,

    From your code it is apparent that you have not really understood what iGadget
    was saying which is indeed correct.

    Array.Clone creates a 'shallow' copy of the array. ArrayInstance.CopyTo will
    perform a shallow copy to the nominated predeclared, predimensioned array.
    Array.Copy will perform a shallow copy between two arrays even when ArraySource
    and ArrayDestination are the same array - great for moving elements inside
    an array.

    Your code could be rewritten as follows:

    Structure Individual
    Public Number As Integer
    Public Chromosone() As Integer
    Public Value As Decimal
    Public Fitness As Decimal
    Public Parent1, Parent2, XSite1, XSite2 As Integer
    End Structure

    Public GenePool() As Individual

    Dim TempPop As Individual()

    'I assume somewhere in here you have actually created GenePool

    ReDim TempPop(GenePool.GetUpperBound(0))

    'TempPop = GenePool.Clone

    'Array.Copy(GenePool, TempPop, UBound(GenePool) + 1)

    For i = 0 To UBound(TempPop)

    TempPop(i).Chromosone = GenePool(i).Chromosone.Clone
    'ReDim TempPop(i).Chromosone(UBound(GenePool(i).Chromosone))

    'For j = 0 To UBound(TempPop(i).Chromosone)

    ' TempPop(i).Chromosone(j) = GenePool(i).Chromosone(j)

    ' TempPop(i).Number = GenePool(i).Number

    'Next

    Next

    This code will perform considerably quicker as the Array gets larger.

    JGCS

  8. #8
    Steve Cochran Guest

    Re: Copy/Duplicate Arrays

    You spelled Chromosome wrong. <G>

    steve

    "David Gaines" <dgaines@atlanticappraisal.com> wrote in message news:3dadce8e$1@tnews.web.devx.com...
    Thanks to all for the suggestions; here is some more information. I've declared an array of structures as shown below. I am now trying to create a clean copy of this array, also as excerpted below.

    BTW, to make it work, I have created a work around where I step through every element of my new array and just set it equal to the value in the old array. It works as it is, but I thought there might be a more "correct" method. I have excerpted relevant portions of my code below.

    Structure Individual
    Public Number As Integer
    Public Chromosone() As Integer
    Public Value As Decimal
    Public Fitness As Decimal
    Public Parent1, Parent2, XSite1, XSite2 As Integer
    End Structure

    Public GenePool() As Individual

    Dim TempPop As Individual()

    ReDim TempPop(UBound(GenePool))

    'TempPop = GenePool.Clone

    'Array.Copy(GenePool, TempPop, UBound(GenePool) + 1)

    For i = 0 To UBound(TempPop)

    ReDim TempPop(i).Chromosone(UBound(GenePool(i).Chromosone))

    For j = 0 To UBound(TempPop(i).Chromosone)

    TempPop(i).Chromosone(j) = GenePool(i).Chromosone(j)

    TempPop(i).Number = GenePool(i).Number

    Next

    Next


    "iGadget" <iGadget_@hotmail.com> wrote in message news:3dad8323$1@tnews.web.devx.com...
    > No it doesn't, it clones the array, so perhaps your problem is that you have
    > an array of references, in which case you have a completely new array of
    > references but they point to the same objects?
    > Perhaps some more information would help, like what is it an array of?
    >
    > Cheers,
    > iGadget
    >
    > "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    > news:3dad78c0$1@tnews.web.devx.com...
    > > Thanks for the response Steve, but that doesn't work.
    > >
    > > Clone apparently also just points the array2 to the array1 memory.
    > >
    > > Thanks again anyway,
    > > Dave
    > >
    > > "Steve Cochran" <scochran@chattanooga.net> wrote in message
    > > news:3dad5cf5$1@tnews.web.devx.com...
    > > try
    > >
    > > array2=array1.clone
    > >
    > > steve
    > >
    > > "David Gaines" <dgaines@atlanticappraisal.com> wrote in message
    > > news:3dad5c3c$1@tnews.web.devx.com...
    > > Greetings,
    > >
    > > I need to replicate an array, but do not want the new array to reference

    > the
    > > original array's memory location. In other words, I need a completely
    > > separate, exact copy of the original array so that I can manipulate its
    > > elements without impacting the original array elements.
    > >
    > > I have tried Array.Copy, but that seems to just point the new array's
    > > elements to the memory location of the original array's elements. Is there

    > a
    > > way to create a clean, new copy of an existing array?
    > >
    > > Thanks,
    > > Dave Gaines
    > >
    > > --
    > > Regards,
    > > Dave Gaines
    > > dgaines@atlanticappraisal.com
    > >
    > > Associate Appraiser
    > > Atlantic Appraisal Company, Inc.
    > > 2355 Huguenard Drive, Suite 202
    > > Lexington, KY 40503
    > > (859) 273-2060, x238
    > >
    > >
    > >

    >
    >


  9. #9
    Dave Gaines Guest

    Re: Copy/Duplicate Arrays

    JGCS,

    Thanks very much for your informative response. You have correctly pegged me
    as a novice programmer; I sure appreciate your guidance and will give your
    code a try.

    Regards,
    Dave Gaines


    "JGCS" <JGCS@Hotmail.com> wrote in message
    news:3dae3995$1@tnews.web.devx.com...
    >
    > "David Gaines" <dgaines@atlanticappraisal.com> wrote:
    > >Greetings,
    > >
    > >I need to replicate an array, but do not want the new array to reference

    > the
    > >original array's memory location. In other words, I need a completely
    > >separate, exact copy of the original array so that I can manipulate its
    > >elements without impacting the original array elements.
    > >
    > >I have tried Array.Copy, but that seems to just point the new array's
    > >elements to the memory location of the original array's elements. Is

    there
    > a
    > >way to create a clean, new copy of an existing array?
    > >
    > >Thanks,
    > >Dave Gaines
    > >
    > >--
    > >Regards,
    > >Dave Gaines
    > >dgaines@atlanticappraisal.com
    > >
    > >Associate Appraiser
    > >Atlantic Appraisal Company, Inc.
    > >2355 Huguenard Drive, Suite 202
    > >Lexington, KY 40503
    > >(859) 273-2060, x238
    > >
    > >

    > Hi David,
    >
    > From your code it is apparent that you have not really understood what

    iGadget
    > was saying which is indeed correct.
    >
    > Array.Clone creates a 'shallow' copy of the array. ArrayInstance.CopyTo

    will
    > perform a shallow copy to the nominated predeclared, predimensioned array.
    > Array.Copy will perform a shallow copy between two arrays even when

    ArraySource
    > and ArrayDestination are the same array - great for moving elements inside
    > an array.
    >
    > Your code could be rewritten as follows:
    >
    > Structure Individual
    > Public Number As Integer
    > Public Chromosone() As Integer
    > Public Value As Decimal
    > Public Fitness As Decimal
    > Public Parent1, Parent2, XSite1, XSite2 As Integer
    > End Structure
    >
    > Public GenePool() As Individual
    >
    > Dim TempPop As Individual()
    >
    > 'I assume somewhere in here you have actually created GenePool
    >
    > ReDim TempPop(GenePool.GetUpperBound(0))
    >
    > 'TempPop = GenePool.Clone
    >
    > 'Array.Copy(GenePool, TempPop, UBound(GenePool) + 1)
    >
    > For i = 0 To UBound(TempPop)
    >
    > TempPop(i).Chromosone = GenePool(i).Chromosone.Clone
    > 'ReDim TempPop(i).Chromosone(UBound(GenePool(i).Chromosone))
    >
    > 'For j = 0 To UBound(TempPop(i).Chromosone)
    >
    > ' TempPop(i).Chromosone(j) = GenePool(i).Chromosone(j)
    >
    > ' TempPop(i).Number = GenePool(i).Number
    >
    > 'Next
    >
    > Next
    >
    > This code will perform considerably quicker as the Array gets larger.
    >
    > JGCS




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center