PDA

Click to See Complete Forum and Search --> : [RESOLVED] Creating arrays.


gjon
Nov 10th, 2006, 10:35 PM
I am still struggling with understanding the creation of objects.
For example.
int[] x = {4,5,6};
int[] y = new int[x.Length];

Why is it that for the first array, we can initialize it without the use of the new keyword yet in the second line, we need the new keyword?
Whats the difference?
Thanks so much.

jmcilhinney
Nov 11th, 2006, 01:12 AM
The first line intialises the value of each element of the array, so you're allowed to use that shorthand. The second line creates an Array object but doesn't intialise any of the elements. If you're not setting a value for any of the elements you certainly can't create the array in any way other than by invoking the constructor.

gjon
Nov 11th, 2006, 09:36 AM
Assigning the values right away tells VS to allocate the memory automatically and fill those indexes. In the second line, the array is created to hold a reference to a location in memory by telling what size it has, but it has no data to fill it with yet. So the first line is a short hand way of doing line two. (newbie gibberish)
Anyhows, a long version of the first line would be:
int[] x = new int[3];
int[0] = 4;
int[1] = 5;
int[2] = 6;

Is all of this description somewhere in the ballpark?

Pirate
Nov 11th, 2006, 12:23 PM
Your situation determines the creation of the array . Sometimes you want to create it predefined with values (ex. first line ) , or you can just create memory spaces for your array but let fill in them later after the creation..

jmcilhinney
Nov 11th, 2006, 06:42 PM
Assigning the values right away tells VS to allocate the memory automatically and fill those indexes. In the second line, the array is created to hold a reference to a location in memory by telling what size it has, but it has no data to fill it with yet. So the first line is a short hand way of doing line two. (newbie gibberish)
Anyhows, a long version of the first line would be:
int[] x = new int[3];
int[0] = 4;
int[1] = 5;
int[2] = 6;

Is all of this description somewhere in the ballpark?Basically yes. The first line says "create an array containing the integers 4, 5 and 6 and assign that array to the variable". The second line says "create an array of the appropriate size to contain 3 integers and assign that array to the variable". The only difference in what happens behind the scenes is that the first method initialises the elements of the array. The fact that the code looks relatively different is a function of the C# language and how it attempts to simplify your life when you're initialising the elements of an array. It doesn't mean that your compiled application works any differently.

gjon
Nov 12th, 2006, 12:13 AM
Thank you, that's the understanding I need to learn as well sometimes. Not just what code does what, but what is actually happening behind the scenes. That just makes more sense to me. I hope practicing it like that will give me a better understanding. or else, I'm just a goofy goober. the latter, more likely...
Thanks again.