Results 1 to 6 of 6
- 08-06-2011, 12:58 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 20
- Rep Power
- 0
- 08-06-2011, 12:59 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 20
- Rep Power
- 0
and can someone explain how array1=array2 works?
- 08-06-2011, 01:19 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What it does, and why changes to array1 affect array2 and vice versa is this:Java Code:int[] array1 = new int[10]; int[] array2 = new int[5]; array1=array2;
Both of the above declarations initialize an array object(the new keyword) on the heap. This object is pointed to by the variable name. When you re-assign, you are basically telling the variable name to point to a different object on the heap. So if you fill array 1 with data, and then set it to array2, you will have lost all the data in array1, since it no longer has a reference pointing to it.
As explained in the above, if both reference(variables) point to the same object on the heap, changes to one will appear to affect the other(it isn't really affecting the other since there is only one object being referenced).
- 12-25-2011, 03:26 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Re: array1 = array2 if array1 be changed then array2 change automatically?
Hello
I got into a problem because of this array pointer thingy, I need to make a copy of an array, then work with the copy, without affecting the original array. how can I accomplish that? how do I make a copy of an array that has different pointers than the original array? I hope someone can answer me soon and thanks
- 12-25-2011, 03:39 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: array1 = array2 if array1 be changed then array2 change automatically?
but as I know : you are making the object array1 = to the object array2;array1=array2;
I think it has no Link with the arrays without []
by my modest experience :)
- 12-25-2011, 05:15 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
SCWCD dumps changed?
By ajincoep in forum Java CertificationReplies: 1Last Post: 07-12-2011, 08:43 AM -
Checking when database was last changed
By Smoke3723 in forum JDBCReplies: 1Last Post: 05-07-2010, 07:47 AM -
Event for jTtextField changed
By itaipee in forum AWT / SwingReplies: 3Last Post: 03-03-2009, 07:55 PM -
Why the panel text changed?
By ottawalyli in forum AWT / SwingReplies: 1Last Post: 12-17-2007, 05:56 AM -
Why the panel text changed?
By ottawalyli in forum SWT / JFaceReplies: 0Last Post: 12-16-2007, 04:16 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks