473,232 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

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
Jun 27 '08 #1
1 1000
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.anders...@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
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Markus Dehmann | last post by:
I have n sets of elements. I want to find elements that occur more than once in more than one set. Maybe the following example shows what I mean: S1 = {1,2,3,2,4} S2 = {2,2,4,5,4} S2 =...
0
by: adavidso | last post by:
I am a bit of a newbie to python, and would like a little advice on how to more efficiently process arrays. I work a lot with satellite data, and have opened and read two binary images into...
5
by: Arjen | last post by:
Hello, I have an array "aPeople" with people objects which I serialize like this: XmlSerializer x = new XmlSerializer( typeof(People) ); TextWriter writer = new StreamWriter( "data.xml" );...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
5
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.