HashMap value() 的问题

发布于 2024-11-02 19:46:16 字数 1122 浏览 0 评论 0原文

我制作瓷砖记忆游戏。我希望当活动开始时初始化带有图片的图块。我尝试这样做:

private void initTiles() {
    // Store name of resources in array
    String[] pictures = new String[] {"tile_circle", "tile_deny", "tile_heart"
                     , "tile_mail", "tile_music", "tile_pin", "tile_splash", "tile_yes"};
    Random rand = new Random();

    int i = 1;

    while(i <= 16) {
        String pic = pictures[rand.nextInt(8)];
        LinkedList<String> list = new LinkedList<String>();

        list = (LinkedList<String>) tilesMapping.values();

         //Check if this picture is already loaded twice
        if(list.indexOf(pic) == -1) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        } else if(list.lastIndexOf(pic) == -1) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        } else if(list.indexOf(pic) == list.lastIndexOf(pic)) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        }

    }
}

但是当我开始游戏时,我收到强制关闭。但是当评论这一行 list = (LinkedList)tilesMapping.values(); 游戏开始。

I make tile memory game. I want when activity start to init tiles with pictures. I try do this:

private void initTiles() {
    // Store name of resources in array
    String[] pictures = new String[] {"tile_circle", "tile_deny", "tile_heart"
                     , "tile_mail", "tile_music", "tile_pin", "tile_splash", "tile_yes"};
    Random rand = new Random();

    int i = 1;

    while(i <= 16) {
        String pic = pictures[rand.nextInt(8)];
        LinkedList<String> list = new LinkedList<String>();

        list = (LinkedList<String>) tilesMapping.values();

         //Check if this picture is already loaded twice
        if(list.indexOf(pic) == -1) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        } else if(list.lastIndexOf(pic) == -1) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        } else if(list.indexOf(pic) == list.lastIndexOf(pic)) {
            tilesMapping.put(Integer.valueOf(i), pic);
            i++;
        }

    }
}

But when I start game I receive force close. But when comment this row list = (LinkedList<String>) tilesMapping.values(); The game starts.

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

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

发布评论

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

评论(2

冷情 2024-11-09 19:46:16

values() 返回一个 Collection,它不需要是 LinkedList,因此可能会发生 ClassCastException。您可以使用复制构造函数获取链表:

List list = new LinkedList(map.values()); // or ArrayList`

values() returns a Collection, it does not need to be a LinkedList, so perhaps a ClassCastException happens. You can obtain a linked list by using a copy-constructor:

List list = new LinkedList(map.values()); // or ArrayList`
高速公鹿 2024-11-09 19:46:16

事实上,代码

LinkedList<String> list = new LinkedList<String>();
list = (LinkedList<String>) tilesMapping.values();

位于 foreach 循环内部,这一事实在我看来并不正确。我想应该是上面的循环。

另外,您可能并不想创建一个 new LinkedList() 然后立即将其丢弃,方法是将对该对象的唯一引用设置为 tilesMapping.values()< /code>... 其中(大概)包含您之前准备的一个,是吗?

这给我们带来了真正的问题:什么是tilesMapping.values(),具体来说它返回什么类型?您不能仅通过类型转换将实际上不是 LinkedList 的内容创建为字符串链接列表。你可以欺骗编译器,但它在运行时仍然不起作用。

干杯。基思.

The fact that the code

LinkedList<String> list = new LinkedList<String>();
list = (LinkedList<String>) tilesMapping.values();

is inside your foreach loop doesn't look right me. I guess it should be above to loop.

Also, you probably didn't mean to create a new LinkedList() and then immediately throw it away, by setting the only reference to that object to the result of tilesMapping.values()... which (presumably) contains one you prepared earlier, yes?

Which brings us to the real problem: What is tilesMapping.values(), and specifically what type does it return? You can't just make something that is NOT really a LinkedList into a linked-list of strings just by type casting it. You can fool the compiler, but it still won't work at runtime.

Cheers. Keith.

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