|
Post by Brutal_Chicken on Dec 12, 2001 17:27:21 GMT -5
Suppose I have something like this;
int[] a = new int[5]; int[] b = a;
b[1] = 3; a[1] = 2;
What is a[1] then? In general terms, since an array is an object does ' int[] b = a; ' mean they share the same address like two objects or does it simply mean b is a copy of a? I'm a little confused because int is a primitive data-type but int[] is an array of ints, an object.
And what of this?
Student[] s1 = new Student[10]; Student[] s2 = s1;
s1[1] = "Brutal_Chicken"; s2[1] = "is an idiot";
Here we have an array of Students (an object) that should share the same address correct? As in s1[1] and s2[1] should both have "is an idiot".
Any clarification would be greatly appreciated.
|
|
|
Post by Sylph on Dec 12, 2001 17:51:38 GMT -5
i believe for the first one, a[1] is 2. maybe u can try it out. the diagram would be like:
a --> 0001 b --> 0001 0001[1] = 3 0001[1] = 2
yeah, the students should share the same address. and s[1] and s[2] should both have "is an idiot". but again, u should test it out to clarify it to yourself.
|
|
|
Post by Evotamer on Dec 12, 2001 17:58:21 GMT -5
Here's what going on from what I'm thining (I could be wrong and as Sylph said you should try it).
You're making an array a of size 5, and allocating it to a spot in memory. You make another array, b and link it to array a. So array b is pointing to array a.
You call: b[1] = 3 so the computer finds b, which is pointing to a... so it changes a[1] = 3
You call: a[1] = 2 the computer finds the address of a[1] and changes it to 2. (So it changes the 3 to a 2 now).
Same thing for your student example, but I think that unless your 'students' were 'Strings' the "is an idiot" won't work.
|
|
|
Post by Brutal_Chicken on Dec 12, 2001 18:42:57 GMT -5
It was an abbreviated example it should've been s1[1] = new Student("whatever"); but I get your drift. I'd check it out but I'm in the process of fixing my computer due to some weird things with my DSL. It knows it's almost winter break and it won't let me play games on it for the duration... From my first example; what would b[1] be then? I'm guessing 3.
|
|
|
Post by bladehunter on Dec 12, 2001 18:49:23 GMT -5
From my understanding, b[1] should also be equal to 2 since a and b point to the same reference location in memory. ;D
|
|
|
Post by Majin_Blues on Dec 12, 2001 19:41:38 GMT -5
From my understanding, b[1] should also be equal to 2 since a and b point to the same reference location in memory. ;D no ifs ands or buts about it... if you change the source both arrays refer to, the way the array shows up will be changed too
|
|
|
Post by Brutal_Chicken on Dec 12, 2001 19:58:50 GMT -5
So for almost all intents and purposes arrays are treated as objects. How about this:
int[] a = new int[3]; int[] b = a; int[] c = b;
b[0] = 1; c[1] = 2; a[2] = 3;
Then a has 1, 2, 3 as its int elements right?
|
|
|
Post by bladehunter on Dec 12, 2001 20:08:30 GMT -5
;D
Yes, you are correct.
|
|