1

Suppose I have a chessboard defined with a HashMap

HashMap <Position,Field> chessboard = new HashMap <Position,Field>();

I declare the Position as

class Position{
  int x;
  int y;
}

When I am trying to make a class for Field object I encounter a problem: the Field should contain a Position because it is defined by it. Ex.

class Field {
  Position pos;
  int color;
  void draw(){
    // draw Field using pos
  }
}

But the Position object is going to be used for HashMap. How could I avoid this redundancy?

4

1 回答 1

2

成本实际上并不是您想的那样:您只持有一个对Position内存中同一对象的引用(在大多数系统上为四个字节)。开销最小;不用担心。

于 2014-06-02T20:13:01.097 回答