CCoder
CCoder

Reputation: 161

Passed ArrayList is getting updated + Java

I have a question that I can't figure out right now. I have a class as Iframe which has the variable "private List pathContainer" to maintain the path of the Iframe.

Given is the code of it,

public class IFrame implements HtmlElement {

private WebElement elem;
private String xpath;
private boolean selected;
private String parentClass;
private boolean isProcessed=false;
private boolean isIframeContent=false;
private List <String> pathContainer;

    public IFrame(WebElement elem) {
    this.elem = elem;
    pathContainer=new ArrayList<String>();
}

I'm passing the path list of a parent iframe to a sub iframe to include it in it's list. But when ever I modify the subIframe path list, the parent Iframe pathlist also gets changed. Given is the code of the function,

public void LoadIFrameNodes(List<String> parentPath){
        IFrame iframe=new IFrame(e);
        List <String> tempPath=new ArrayList<String>();
        iframe.setPathContainer(parentPath); //assigning parent path in subIframe list
        tempPath=iframe.getPathContainer();
        tempPath.add(iframe.getXpath());  // add another value to subIframe
        iframe.setPathContainer(tempPath); //setting the changed list as the subIframe
   }

Once the the subIframe is set with the new values, the passed parentPath list also gets changed with the new values. I don't the passed list to be updated. Please let me know where it went wrong?

Upvotes: 0

Views: 117

Answers (4)

Code-Apprentice
Code-Apprentice

Reputation: 83527

The problem is that you have a several reference variables which point at the same object. I suggest that you do some reasearch to learn how reference variables work.

To fix the problem, you need to make a copy of the List immediately before or after it is passed to the method.

Upvotes: 1

helion3
helion3

Reputation: 37381

You need to make a copy of the list or pass the information differently because each reference is pointing to the original, so any change you make will affect the original.

StackOverflow has several examples of cloning arraylists:

clone(): ArrayList.clone() I thought does a shallow copy

ArrayList shallow copy iterate or clone()

How to clone ArrayList and also clone its contents?

Upvotes: 0

Jimmy Lee
Jimmy Lee

Reputation: 1011

In Java, when passed objects are changed, the changes are persistant even when the method returns. When you set the iframe's pathcontainer to be the parentPath and then take the path back out, you get a reference to the same parentPath. When you change it, it persists. If you don't want this, make a copy.

You can do this with the call

List<String> tempPath = new ArrayList<String>(parentPath);
iframe.setPathContainer(tempPath);

Upvotes: 1

Thilo
Thilo

Reputation: 262504

iframe.setPathContainer(parentPath); //assigning parent path in subIframe list
tempPath=iframe.getPathContainer();

Unless those getter/setters make defensive copies (which usually they don't), your tempPath just points to the same object as parentPath.

You need to make a copy of the list before updating it.

 final List<String> tempPath=new ArrayList<String>(parentPath);
 tempPath.add(iframe.getXpath());  
 iframe.setPathContainer(tempPath);

Upvotes: 3

Related Questions