|
-
Nov 10th, 2006, 11:35 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Creating arrays.
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.
-
Nov 11th, 2006, 02:12 AM
#2
Re: Creating arrays.
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.
-
Nov 11th, 2006, 10:36 AM
#3
Thread Starter
Hyperactive Member
Re: Creating arrays.
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?
-
Nov 11th, 2006, 01:23 PM
#4
Sleep mode
Re: Creating arrays.
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..
-
Nov 11th, 2006, 07:42 PM
#5
Re: Creating arrays.
 Originally Posted by gjon
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.
-
Nov 12th, 2006, 01:13 AM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] Creating arrays.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|