HashMap value() 的问题
我制作瓷砖记忆游戏。我希望当活动开始时初始化带有图片的图块。我尝试这样做:
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
游戏开始。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
values()
返回一个Collection
,它不需要是LinkedList
,因此可能会发生ClassCastException
。您可以使用复制构造函数获取链表:values()
returns aCollection
, it does not need to be aLinkedList
, so perhaps aClassCastException
happens. You can obtain a linked list by using a copy-constructor:事实上,代码
位于 foreach 循环内部,这一事实在我看来并不正确。我想应该是上面的循环。
另外,您可能并不想创建一个
new LinkedList()
然后立即将其丢弃,方法是将对该对象的唯一引用设置为tilesMapping.values()< /code>... 其中(大概)包含您之前准备的一个,是吗?
这给我们带来了真正的问题:什么是tilesMapping.values(),具体来说它返回什么类型?您不能仅通过类型转换将实际上不是
LinkedList
的内容创建为字符串链接列表。你可以欺骗编译器,但它在运行时仍然不起作用。干杯。基思.
The fact that the code
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 oftilesMapping.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 aLinkedList
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.