Results 1 to 3 of 3
Thread: Question about array
- 09-18-2010, 03:06 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
Question about array
I wanna know are the following cases implement the same thing?
Case A.
Case B.Java Code:int arrayA[] = new int[10]; int arrayB[] = new int[10]; arrayA = arrayB;
In both case, I found that arrayA contains the same elements as arrayB.Java Code:int arrayA[] = new int[10]; int arrayB[] = new int[10]; for (int i = 0 ; i < arrayA.length; i++) { arrayA[i] = arrayB[i]; }
However, is there any difference(meaning or implementation.....) between these two cases?
Thanks for your help:)
- 09-18-2010, 03:35 PM #2
No they are different.
arrayA = arrayB;
The above replaces/loses the array arrayA used to point to, replacing it with a reference to arrayB.
If you make a change to arrayA it can be seen in arrayB because there is only one array.
In the second case if after the loop you make a change to arrayA it will NOT be seen in arrayB.
Write a small test program to see this.
Exactly. arrayA points to the SAME array as arrayB. The initial contents of arrayA are gone.In both case, I found that arrayA contains the same elements as arrayB.
Your code is the same as:
String s1 = "1";
String s2 = "2";
s1 = s2; // the reference to "1" is replaced with one to "2". The "1" is gone.Last edited by Norm; 09-18-2010 at 03:38 PM.
- 09-18-2010, 03:55 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Array question
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 06-11-2010, 01:41 AM -
Array Question
By sc001 in forum New To JavaReplies: 1Last Post: 02-14-2010, 04:57 AM -
array question
By dazednconfused in forum New To JavaReplies: 4Last Post: 09-15-2009, 05:44 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM -
file/ array question
By mayhewj7 in forum New To JavaReplies: 10Last Post: 02-18-2009, 03:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks