在本地定义的哈希表上执行操作时,分配给本地哈希表的另一个类的全局哈希表也会对其执行这些操作。我不确定这是否是一个显而易见的简单问题,但我找不到任何问题。Java:本地哈希表覆盖全局哈希表
public void redraw(String[] sNewEntity,Hashtable<String,Entity> h){
Hashtable<String,Entity> htEntities=h;
htEntities.remove(sNewEntity[0]);
//many lines of code that ensures that the removal doesn't invalidate anything
if(validation==true){
m_HomeFrame.graph=new mxGraph();
m_HomeFrame.myGraphComponent = new mxGraphComponent(m_HomeFrame.graph);
m_HomeFrame.setScroll(m_HomeFrame.myGraphComponent);
m_HomeFrame.m_Tree.h_entityTable=htEntities;
}
}
这是函数。所有带“m _”的变量都是全局变量。此方法接受一个字符串数组和散列表,并删除具有字符串数组第一个项的键的散列表项。问题是全局散列表(m_HomeFrame.m_Tree.h_entityTable)一旦命中(htEntities.remove(sNewEntity [0]);)就会立即更新。有没有办法从全局变量中分离出htEntities?此外,道歉不使用泛型。我经常在搜索hashtable的时候看到这种情况。
这种行为被称为*副作用*。 – JonK