Monday 4 November 2019

java - Access the pointer to an ArrayList?

I have an ArrayList in Java. Now, I want to access the pointer to the said list. This is so that I can make a variable out of the pointer and make operations using it. Any advise on how to do this?



What I want to do exactly is similar to making "list" below always point to the value of "someOtherList".



import java.util.ArrayList;
import java.util.List;


public class ListContainingObject {

private List list;

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;

}

public static void main(String args[]){
ListContainingObject listContainingObject= new ListContainingObject();

System.out.println(listContainingObject.getList());

List someOtherList = new ArrayList();
listContainingObject.setList(someOtherList);
System.out.println(listContainingObject.getList());

System.out.println(someOtherList);

someOtherList.add("1");
System.out.println(listContainingObject.getList());
System.out.println(someOtherList);

//I want the two below to have the same value
someOtherList = new ArrayList();
System.out.println(listContainingObject.getList());
System.out.println(someOtherList);

}
}


The seemingly appropriate workaround would be to call the setters again explicitly like below.



public class ListContainingObject {
public void resetList(List toReset) {
this.list = new ArrayList();
toReset = this.list;

}
}
listContainingObject.resetList(someOtherList);


But this would lead to another problem wherein I want solcowiab.getList() and listContainingObject.getList() below to always be the same, assuming that I don't have the source code for SomeOtherListContainingObjectWhichIsABlackBox.



import java.util.ArrayList;
import java.util.List;


public class ListContainingObject {

private List list;

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;

}

public static void main(String args[]) {
ListContainingObject listContainingObject = new ListContainingObject();
SomeOtherListContainingObjectWhichIsABlackBox solcowiab = new SomeOtherListContainingObjectWhichIsABlackBox();
List aNewList = new ArrayList();
aNewList.add("1");
solcowiab.setList(aNewList);
listContainingObject.setList(solcowiab.getList());
System.out.println(listContainingObject.getList());

System.out.println(solcowiab.getList());

//The two below will have the same value but
//at some point "list" did not point to "someOtherList"
solcowiab.aMethodThatSupposedlyWontCallSetList();
listContainingObject.setList(solcowiab.getList());
System.out.println(listContainingObject.getList());
System.out.println(solcowiab.getList());
}
}


class SomeOtherListContainingObjectWhichIsABlackBox {

private List someOtherList;

public List getList() {
return someOtherList;
}

public void setList(List list) {

this.someOtherList = list;
}

public void aMethodThatSupposedlyWontCallSetList() {
//this one won't be observed by ListContainingObject
setList(new ArrayList());
getList().add("2");
//do some other stuff

//only this assignment will be controlled by ListContainingObject's main method

setList(new ArrayList());
}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...