Bat*_*mos 1 java variables final immutability
我有一个Path类,我认为它是不可变的.在另一个名为Test的类中,我有一个Path对象的最终引用.
然而,在构造函数和getter方法之间,即使Path对象是不可变的并且引用是final,Path对象也会更改.我知道这一点,因为Path中int数组节点的长度从构造函数变为getter.看起来这个对象完全是一个完全不同的对象.
我的程序是多线程的,但我已尝试使用单个线程,但没有解决问题.
这是不可变的Path类
public class Path implements Iterable<Point> {
private final int[] nodes;
private final double distance;
public Path(Scenario scenario, int gateway, int sensor){
this.scenario = scenario;
nodes = new int[2];
nodes[1] = -gateway - 1;
nodes[0] = sensor;
distance = scenario.DISTANCE_GATEWAY_SENSOR[gateway][sensor];
}
public Path(Path base, int newSensor){
scenario = base.scenario;
//Copy the old path. These are rigid structures so that we do not need to deep copy
nodes = new int[base.nodes.length + 1];
for(int i = 0; i < base.nodes.length; i++)
nodes[i + 1] = base.nodes[i];
nodes[0] = newSensor;
distance = base.distance + scenario.DISTANCE_SENSOR_SENSOR[newSensor][nodes[1]];
}
public Path(Scenario scenario, int[] nodes, boolean isSensor, double distance){
this.scenario = scenario;
this.distance = distance;
this.nodes = Arrays.copyOf(nodes, nodes.length);
if(!isSensor)
for(int i = 0; i < this.nodes.length; i++)
this.nodes[i] = -this.nodes[i] -1;
}
@Override
public Iterator<Point> iterator() {
return new PointIterator();
}
public class PointIterator implements Iterator<Point>{
private int next = -1;
@Override
public boolean hasNext() {
return next + 1 < nodes.length;
}
@Override
public Point next() {
int p = nodes[++next];
if(p >= 0)
return scenario.SENSOR_LOCATION[p];
return scenario.CS_LOCATION[-p - 1];
}
@Override
public void remove() {
throw new IllegalAccessError("This method is not supported");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是Test类(最后引用Path类)
public class Test {
private final Path gatewayTour;
public Test(Scenario scenario, boolean[] chosenGateway){
distanceFitness = 0;
Point current = scenario.SINK_LOCATION;
boolean visited[] = new boolean[scenario.CONFIG.NUM_CS];
int nextGateway;
LinkedList<Integer> order = new LinkedList<>();
do {
double minimumDistance = Double.MAX_VALUE;
nextGateway = -1;
for(int i = 0; i < scenario.CONFIG.NUM_CS; i++)
if(!visited[i] && CHOSEN_GATEWAYS[i] && scenario.CS_LOCATION[i].isCloserThan(minimumDistance, current)) {
nextGateway = i;
minimumDistance = scenario.CS_LOCATION[i].distance(current);
}
if(nextGateway >= 0) {
distanceFitness += minimumDistance;
visited[nextGateway] = true;
order.add(nextGateway);
current = scenario.CS_LOCATION[nextGateway];
}
} while(nextGateway >= 0);
int path[] = new int[order.size()];
Iterator<Integer> it = order.iterator();
for(int i = 0; i < order.size(); i++)
path[i] = it.next().intValue();
gatewayTour = new Path(scenario, path, false, distanceFitness);
}
public Path getGatewayTour(){
//Here, the gatewayTour object has changed and does not have the same content as in the constructor
return gatewayTour;
}
}
Run Code Online (Sandbox Code Playgroud)
我的程序中有什么东西允许对象改变吗?我会更精确:是否有任何东西允许在Path类中的int数组"节点"改变长度?因为这是真正的问题.
[编辑]:我的测试有缺陷,这使我相信我的'节点'数组的值发生了变化.感谢所有指出我的代码中存在缺陷或可能改进的人.
我会接受AlexR的答案,因为他指出可以改变最终阵列中的各个元素; 我不知道的东西,这有助于解决问题.
Word final表示无法更改标有此单词的引用.这并不意味着无法更改引用的对象.
这意味着Path通过更改其字段来更改实例没有问题.是的,你是对的,你的领域也是最终的.但是让我们检查它们:
private final int[] nodes;
private final double distance;
private final Scenario scenario;
Run Code Online (Sandbox Code Playgroud)
distance是原始的,因此在初始化期间分配后确实无法更改.nodes是一个数组,即对象.数组本身不能更改,即引用引用相同的数组.但是,您可以更改数组的元素.
scenario也是对象.您尚未在Scenario此处发送类,但如果可以更改此类的字段,则可以更改此对象.