Results 1 to 5 of 5
Thread: 2 variables get linked
- 11-30-2011, 06:32 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
2 variables get linked
//Hi,
//I have a piece of code in which I created a tempArray that I fill with data from a file.
arrayList <someDataType> tempArr = new arrayList <someDataType>(); //local variable
//After I hit a delimiter in the file, I want to use this temp variable to set a parameter in another object:
AnotherObj tempAnotherObj = new AnotherObj(); //local variable
tempAnotherObj .setparam(tempArr );
//finally I add tempAnotherObj to an array of AnotherObj which is a private variable of the class this method belongs to:
arrAnotherObjAr.add(tempAnotherObj );
//now I clear tempArr from step 1 because I want to fill with new data until i hit a new delimiter again
tempArr.clear();
//As soon as that happens all the data both in the tempAnotherObj and arrAnotherObjAr lose the data recently acquired (tested with prints on both side of clear line).
//It is as if tempArr gets perma linked to tempAnotherObj and arrAnotherObjAr. I just want to grab its data and move on. Is there something I could do to break this dependency?
//Thanks in advance.
-
Re: 2 variables get linked
Please show us a small test program that demonstrates your problem. Please be sure to use code tags when posting code, and to separate your question from your code so the one doesn't clutter the other.
Best of luck.
- 11-30-2011, 07:07 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
Re: 2 variables get linked
Java Code:double degX = 0, degY = 0; coordsLine = br.readLine(); ArrayList <LatLon> tempArray = new ArrayList<LatLon>(); SurfacePolygon tempSurfacePolygon = new SurfacePolygon(); int j=0,k=0, last_K=0; while(coordsLine != null) { if (coordsLine.startsWith(";")) //polygon delimiter { tempSurfacePolygon.setLocations(tempArray); System.out.println("tempSurfacePolygon coords=" + tempSurfacePolygon.getOuterBoundary()); polygonAr.add(tempSurfacePolygon); System.out.println("polygonAr " + j + " coords=" + ((SurfacePolygon) polygonAr.get(j)).getOuterBoundary()); tempArray.clear(); //<-----------------------------------------------------------------------------------------AFTER THIS IS EXECUTED BOTH tempSurfacePolygon AND polygonAr LOSE THE DATA System.out.println("tempSurfacePolygon coords=" + tempSurfacePolygon.getOuterBoundary()); System.out.println("polygonAr " + j + "after clear coords=" + ((SurfacePolygon) polygonAr.get(j)).getOuterBoundary()); j++; //count the number of polygons } else { degX = Double.parseDouble(coordsLine.substring(0, coordsLine.indexOf(" ")-1)); degY = Double.parseDouble(coordsLine.substring(coordsLine.indexOf(" ")+1,coordsLine.length() )); tempArray.add(LatLon.fromDegrees(degY,degX)); } coordsLine = br.readLine(); } System.out.println("polygonAr size is " + polygonAr.size()); for (int i=0; i<j; i++) { System.out.println("polygonAr " + i + " coords=" + ((SurfacePolygon) polygonAr.get(i)).getOuterBoundary()); }Last edited by JavaNoob84; 11-30-2011 at 07:10 AM.
- 11-30-2011, 09:22 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: 2 variables get linked
What you are seeing is the logic of references. When you call tempSurfacePolygon.setLocations(tempArray) then the polygon gets a reference to the array. If you later do anything with the array (like clear it) everybody who has a reference to the array will "see" the changes. It works the other way around as well: if the polygon happened to do something to the array (like add elements) then those changes would have an effect on the method you posted.AFTER THIS IS EXECUTED BOTH tempSurfacePolygon AND polygonAr LOSE THE DATA
(You don't actually say what setLocations() does - but I'm assuming it just keeps a local copy of the reference. Ie assigns the argument's value to some local value)
The solution is probably for setLocations() to create its own array into which it copies the latlon values in the array whose reference is passed.
- 11-30-2011, 09:41 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
help with linked list
By TopNFalvors in forum New To JavaReplies: 8Last Post: 03-28-2011, 04:31 PM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
Linked List Help
By tjoney in forum New To JavaReplies: 3Last Post: 02-18-2011, 01:09 AM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks