假设我有一个用HashMap定义的棋盘
HashMap <Position,Field> chessboard = new HashMap <Position,Field>();
我声明Position
为
class Position{
int x;
int y;
}
当我尝试为Field
对象创建类时,遇到一个问题:该对象Field
应包含一个,Position
因为它是由它定义的。前任。
class Field {
Position pos;
int color;
void draw(){
// draw Field using pos
}
}
但是Position对象将用于HashMap。如何避免这种冗余?
代价实际上并不是您想的那样:您只对Position
内存中的同一对象持有一个引用(在大多数系统上为四个字节)。开销很小。甚至不用担心。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句