Forums Register Login

arrays - different size

+Pie Number of slices to send: Send
Why does this code compile and even run ? Arrays are of different size!

+Pie Number of slices to send: Send
The following line of code...



merely assign the finalvalue reference to the same array object referenced by the initial variable. It does not do an array copy. After the operation both variables will reference the same array object.

Since both are int array varables, the assignment is legal.

Henry
+Pie Number of slices to send: Send
The key is that the variables "initial" and "finalvalue" are both of the same type: int array. They can reference any int array assigned (or re-assigned) to them.

The size of an array has to do with the object created -- not the variable it's assigned to.

Here, an int array is instantiated to length 3, and another int array instantiated to length 2. Initially, references to these objects are assigned to the variables "initial" and "final" value respectively. But there's no reason these can't be switched.

However, in your code, I think you'll find that both of these variables end up referencing the same object. Can you see why?
+Pie Number of slices to send: Send
 



If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the refrence you're assigning it to.



So I do not understand the above statement ? The code seems to work differently.
+Pie Number of slices to send: Send
Dear Readers,


If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the refrence you're assigning it to.



I think here the same dimension means with respect to 1-D, 2-D and so on. It does not refer to the size of the array as in your case the arrays are of size 2 and 3 respectively BUT the important thing to remember is that both have the same dimension.

I really hope that what I have said is right. If not, please correct me.
+Pie Number of slices to send: Send
Hi Shyam,

Now that you mention it... it does look that you are correct!

Thanks
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1092 times.
Similar Threads
Putting Binary Search Tree into an array
inhertiance question
A simple question
Javaprepare Question
Java Rule Round-Up
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:25:00.