【发布时间】:2011-05-02 20:37:08
【问题描述】:
我有一个for循环,我检查HashMap中是否有某个键,如果键不存在,它应该在HashMap中放置一个新的关联。问题是它放置了关联,但是在循环的下一次迭代中关联消失了!没看懂!
public void DrawBoard(ArrayList<Integer[]> BoardList, int FrameX, int FrameY){
StdDraw.setCanvasSize(FrameX*50, FrameY*50);
StdDraw.clear(new Color(0,0,0));
StdDraw.setXscale(0, FrameX);
StdDraw.setYscale(FrameY, 0);
Map<Integer[], Color> Coloring = new HashMap<Integer[], Color>();
for(int i = 0; i < BoardList.size(); i++){
Integer[] Rectangle = BoardList.get(i);
Random rand = new Random();
Integer[] ColorCheck = {Rectangle[2], Rectangle[3]};
if(Coloring.containsKey(ColorCheck)){
StdDraw.setPenColor(Coloring.get(ColorCheck));}
else{
Color newColor = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
Coloring.put(ColorCheck, newColor);
StdDraw.setPenColor(newColor);
}
double x1 = Rectangle[0];
double y1 = Rectangle[1];
double x2 = Rectangle[2];
double y2 = Rectangle[3];
StdDraw.filledRectangle(x1+(x2/2), y1+(y2/2), x2/2, y2/2);
}
}
【问题讨论】:
-
仅作记录,在 Java 中变量/成员通常是小写的。你有 .Net 背景?
-
我没有背景,这是我在大学的第一个任务。因此,礼节略显凌乱。