我有一个for循环,在那里我检查是否有一个HashMap中的某个键,如果该键不存在,它应该在HashMap中放入一个新的关联。问题在于它把关联放在了一起,但是通过循环的下一次迭代,关联就消失了!我不明白!Java HashMap放在for循环
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变量/成员通常是lowercased。你有.Net背景吗? – extraneon 2010-10-30 16:54:24
我没有背景,这是我在大学的第一份作业。因此,稍微凌乱的礼仪。 – Lauge 2010-10-30 18:12:33