Results 1 to 5 of 5
  1. #1
    JavaNoob84 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    JavaNoob84 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: 2 variables get linked

    AFTER THIS IS EXECUTED BOTH tempSurfacePolygon AND polygonAr LOSE THE DATA
    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.

    (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.

  5. #5
    JavaNoob84 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: 2 variables get linked

    Thanks. I got around the problem by using a .clone() on a tempArray and it was fine after that. Is that a standard Java solution? Kinda want to conform to established coding conventions.

Similar Threads

  1. help with linked list
    By TopNFalvors in forum New To Java
    Replies: 8
    Last Post: 03-28-2011, 04:31 PM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Linked List Help
    By tjoney in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 01:09 AM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •