TreeMap foreach 不改变值对象
TreeMap foreach doesn't change value object
所以我有一个 TreeMap<Integer, Transmitter>
并通过 foreach 我试图修改发射器的内部属性,但感觉它正在复制 TreeMap 中的对象,因为它没有更改 TreeMap 中的值。
我的 foreach 代码:
for (TreeMap<Integer, Transmitter> current : transmitterDiagnosticMap.values()) {
for (Transmitter t : current.values()) {
String transmitterError = t.printErrorReport(date, appContext);
if (transmitterError != null)
stringsErrorsAndWarnings.add(transmitterError);
}
}
我的打印错误报告代码:
public String printErrorReport(String data, Context context) {
String output = null;
if (this.writeOnReport()) { // This is the function that will modify the object
output = data + " - " + this.getTension();
}
return output;
}
// This is the method that tells whether or not the report will be written, and changes the variable lastStatus if necessary
private boolean writeOnReport() {
if (this.status > 0) {
if (this.lastStatus == 0 || this.lastStatus != this.status) {
this.lastStatus = this.status;
return true;
}
return false;
} else {
this.lastStatus = 0;
return false;
}
}
我注意到 Transmitter t
实际上将值从 lastStatus = 0
更改为 lastStatus = 1
,但 TreeMap 中没有任何更改。
您必须使用迭代器来改变 TreeMap 中的值。使用 current.values()
将创建一个副本而不是改变对象。
您需要遍历 TreeMap 的键并更新值。
for (TreeMap<Integer, Transmitter> current : transmitterDiagnosticMap.values()) {
for (Map.Entry<Integer, Transmitter> entry : current.entrySet()) {
Transmitter t = entry.getValue();
String transmitterError = t.printErrorReport(date, appContext);
if (transmitterError != null)
stringsErrorsAndWarnings.add(transmitterError);
entry.setValue(t);
}
}
所以我有一个 TreeMap<Integer, Transmitter>
并通过 foreach 我试图修改发射器的内部属性,但感觉它正在复制 TreeMap 中的对象,因为它没有更改 TreeMap 中的值。
我的 foreach 代码:
for (TreeMap<Integer, Transmitter> current : transmitterDiagnosticMap.values()) {
for (Transmitter t : current.values()) {
String transmitterError = t.printErrorReport(date, appContext);
if (transmitterError != null)
stringsErrorsAndWarnings.add(transmitterError);
}
}
我的打印错误报告代码:
public String printErrorReport(String data, Context context) {
String output = null;
if (this.writeOnReport()) { // This is the function that will modify the object
output = data + " - " + this.getTension();
}
return output;
}
// This is the method that tells whether or not the report will be written, and changes the variable lastStatus if necessary
private boolean writeOnReport() {
if (this.status > 0) {
if (this.lastStatus == 0 || this.lastStatus != this.status) {
this.lastStatus = this.status;
return true;
}
return false;
} else {
this.lastStatus = 0;
return false;
}
}
我注意到 Transmitter t
实际上将值从 lastStatus = 0
更改为 lastStatus = 1
,但 TreeMap 中没有任何更改。
您必须使用迭代器来改变 TreeMap 中的值。使用 current.values()
将创建一个副本而不是改变对象。
您需要遍历 TreeMap 的键并更新值。
for (TreeMap<Integer, Transmitter> current : transmitterDiagnosticMap.values()) {
for (Map.Entry<Integer, Transmitter> entry : current.entrySet()) {
Transmitter t = entry.getValue();
String transmitterError = t.printErrorReport(date, appContext);
if (transmitterError != null)
stringsErrorsAndWarnings.add(transmitterError);
entry.setValue(t);
}
}