more about arrays

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

    more about arrays

    Hello!

    I have read in a book the following.
    The last line of this method Shuffle shown below uses the CopyTo() method of
    the System.Array class(used whenever you create an array) to copy each of
    the Cards in newDeck back into cards. This means that you are using the same
    set of cards object
    in the same cards object rather than creating any new instances. If you had
    instead used cards = newDeck, then you would be replacing the object
    instance referred to by cards with another. This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!

    Now to my question is it safe to replace newDeck.CopyTo( cards, 0);
    with cards = newDeck in the last row in the method Shuffle below..

    The text above is mentioned something about not being safe. If you had
    instead used cards = newDeck, then you would be replacing the object
    instance referred to by cards with another. This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!

    So what is the author trying to say with this This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!


    public void Shuffle()
    {
    Card[] newDeck = new Card[52];
    bool[] assigned = new bool[52];
    Random rand = new Random();

    for (int i = 0; i < 52; i++)
    {
    int destCard = 0;
    bool foundCard = false;

    while (foundCard == false)
    {
    destCard = rand.Next(52);
    if (assigned[destCard] == false)
    foundCard = true;
    }
    assigned[destCard] = true;
    newDeck[destCard] = cards[i];
    }
    newDeck.CopyTo( cards, 0);
    }

    //Tony


  • jake

    #2
    Re: more about arrays

    The quote means that after execution of the statement "Cards =
    newDeck", Cards will be pointing to (referencing) a different object,
    and thus, any retained references to the old object elsewhere in the
    code will not be updated.

    Example:
    class a { ... }
    a[] c = new a[1]; // <----- c is now pointing to an array of objects
    a d = c[0]; // <----- d is now pointing to an element in the object
    array
    c = new a[1]; // <---- c is now pointing to a different array of
    objects
    // d is still pointing to someplace inside an instant no longer
    pointed to by c

    Now, this can spell advantage or disaster depending upon whether you
    deem such behavior suitable. If there are other references in your
    code that need to point to the new instant then use CopyTo() instead.
    If you want the other references to retain pointers to the old instant
    then, by all means, use Cards = newDeck, it is faster than CopyTo();

    Hope this helps,
    jake


    On Jun 7, 4:49 pm, "Tony Johansson" <johansson.ande rs...@telia.com >
    wrote:
    Hello!
    >
    I have read in a book the following.
    The last line of this method Shuffle shown below uses the CopyTo() method of
    the System.Array class(used whenever you create an array) to copy each of
    the Cards in newDeck back into cards. This means that you are using the same
    set of cards object
    in the same cards object rather than creating any new instances. If you had
    instead used cards = newDeck, then you would be replacing the object
    instance referred to by cards with another. This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!
    >
    Now to my question is it safe to replace newDeck.CopyTo( cards, 0);
    with cards = newDeck in the last row in the method Shuffle below..
    >
    The text above is mentioned something about not being safe. If you had
    instead used cards = newDeck, then you would be replacing the object
    instance referred to by cards with another. This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!
    >
    So what is the author trying to say with this This could cause problems if
    code elsewhere was retaining a reference to the original cards instance -
    which wouldn't be shuffled!
    >
    public void Shuffle()
    {
    Card[] newDeck = new Card[52];
    bool[] assigned = new bool[52];
    Random rand = new Random();
    >
    for (int i = 0; i < 52; i++)
    {
    int destCard = 0;
    bool foundCard = false;
    >
    while (foundCard == false)
    {
    destCard = rand.Next(52);
    if (assigned[destCard] == false)
    foundCard = true;
    }
    assigned[destCard] = true;
    newDeck[destCard] = cards[i];
    }
    newDeck.CopyTo( cards, 0);
    >
    }
    >
    //Tony

    Comment

    Working...