代碼如下
//dataPage對象包含了List<Statistictime>,此處想改變dataPage裏List裏Statistictime的內容
//將job_state字段從code如yjs轉換爲name如"已結束"
GroupJobService groupJobService = new GroupJobService();
//獲取code與name的映射如key="yjs",value="已結束"
Map<String, String> typeMapping = groupJobService.getTypeMapping();
List<Statistictime> list = dataPage.getList();
String jobState = null;
for (Statistictime statistictime : list) {
jobState = statistictime.getStr("job_state");
jobState = jobState!=null&&!"".equals(jobState.trim()) ? jobState.trim() : null;
if(jobState!=null && typeMapping.containsKey(jobState)) {//typeMapping包含該key
statistictime.set("job_state", typeMapping.get(jobState));
statistictime.set("JOB_STATE", typeMapping.get(jobState));
}
jobState = null;
}
從上面代碼可以看出,我們只是直接從dataPage獲取相應的List,並作出數據處理後並沒有將List放回dataPage裏,但dataPage的內容已經改變了,以下的簡單demo可以驗證
public class MyTest {
@Test
public void testIt() {
A a = new A();
a.setA(10);
System.out.println(a.getA());
go(a);
System.out.println(a.getA());
}
public void go(A a) {
a.setA(5);
}
}
public class A{
private int a;
public void setA(int a){
this.a=a;
}
public int getA(){
return this.a;
}
}
上面的代碼的運行結果爲:10,5
從而驗證了傳遞引用相當於傳遞指針的值,該值是對象在堆內存裏的地址.