Java HashMap放入一个for循环中

发布于 2024-09-29 21:27:45 字数 1159 浏览 0 评论 0原文

我有一个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);
    }
}

I have a for loop, where i check if there is a certain key in a HashMap, if the key is not there, it should put a new association in the HashMap. The problem is that that it puts the association, but by the next iteration of the loop the association is gone! I don't get it!

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2024-10-06 21:27:45

正如 Nikita 所说,数组不实现逐个值的等于。它只是一个对象等于。

如果您想使用此实现,您应该使用带有自定义比较器的 Map(如 TreeMap)并使用例如 Arrays.equals 中的实现那个比较器。这样,colorCheck 的元素也会被检查(数组内容),而不是数组引用。

As Nikita said, arrays don't implement an value-by-value equals. It's just an object equals.

If you want to use this implementation you should use a Map with a custom comparator (like a TreeMap) and use for instance Arrays.equals in the implementation of that comparator. That way the elememts of colorCheck are also checked (array content), and not so much the array reference.

無處可尋 2024-10-06 21:27:45

Java 中的数组不提供 equals 方法的实现。例如,array1.equals(array2) 将始终为 false,即使两个数组包含相同数量的相同对象。因此,您不能将它们用作地图键:map.containsKey 不起作用。

尝试使用列表:Arrays.asList(1, 2)。或者创建一个特殊的“pair”对象,因为您只在数组中保存两个元素。

Arrays in Java don't provide an implementation of equals method. E.g., array1.equals(array2) will always be false, even if both arrays contain equal number of the same objects. Therefore, you can't use them as map keys: map.containsKey won't work.

Try lists instead: Arrays.asList(1, 2). Or create a special 'pair' object, since you only hold two elements in array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文