由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - [转载] Java 1.5 Generic 问题
相关主题
相关话题的讨论汇总
话题: string话题: hashmap话题: object话题: generic话题: java
进入Java版参与讨论
1 (共1页)
T*******x
发帖数: 8565
1
【 以下文字转载自 Programming 讨论区,原文如下 】
发信人: TheMatrix (TheMatrix), 信区: Programming
标 题: Java 1.5 Generic 问题
发信站: Unknown Space - 未名空间 (Sat Jun 4 19:54:48 2005) WWW-POST
比如我有一个HashMap,它的Key是String,Value可以是任意的类型。
我就用HashMap来声明它。然后我插入一个key-value pair
但是这个value本身是另一个HashMap,这个HashMap的Key和Value都是
String。我就用HashMap来声明它。插入没有问题。
但当我取出的时候,我从HashMap中取出的是一个Object
类型。但我知道它实际上是一个HashMap。我怎么把它
cast成一个HashMap呢?总是有warning。或者怎样可以
设计数据结构,
m******t
发帖数: 2416
2

I don't think you can get rid of the warning - as long as you keep
using HashMap. If you are sure all the values
are going to be HashMap, you can do:
HashMap>
which will prevent the warning.
Otherwise, the best the compiler knows about your value type at
retrieval time is that it's an Object, and when you downcast it,
the compiler will always give a warning.

【在 T*******x 的大作中提到】
: 【 以下文字转载自 Programming 讨论区,原文如下 】
: 发信人: TheMatrix (TheMatrix), 信区: Programming
: 标 题: Java 1.5 Generic 问题
: 发信站: Unknown Space - 未名空间 (Sat Jun 4 19:54:48 2005) WWW-POST
: 比如我有一个HashMap,它的Key是String,Value可以是任意的类型。
: 我就用HashMap来声明它。然后我插入一个key-value pair
: 但是这个value本身是另一个HashMap,这个HashMap的Key和Value都是
: String。我就用HashMap来声明它。插入没有问题。
: 但当我取出的时候,我从HashMap中取出的是一个Object
: 类型。但我知道它实际上是一个HashMap。我怎么把它

z**e
发帖数: 2
3
I agree with magicfact.
The following thread is also helpful with this topic.
http://forum.java.sun.com/thread.jspa?threadID=612574&messageID=3382719

【在 m******t 的大作中提到】
:
: I don't think you can get rid of the warning - as long as you keep
: using HashMap. If you are sure all the values
: are going to be HashMap, you can do:
: HashMap>
: which will prevent the warning.
: Otherwise, the best the compiler knows about your value type at
: retrieval time is that it's an Object, and when you downcast it,
: the compiler will always give a warning.

T*******x
发帖数: 8565
4
谢谢。
这个问题我再想一想。
forum上的讨论我看了,还得想一想。
我看了一点generic tutorial,觉得好像搞得太复杂了,没看进去。

【在 z**e 的大作中提到】
: I agree with magicfact.
: The following thread is also helpful with this topic.
: http://forum.java.sun.com/thread.jspa?threadID=612574&messageID=3382719

w*******g
发帖数: 9932
5
whenever you have downcast in Java, it will have dynamic checks.
If you use generic consistently, there are very few places you need downcast.

【在 T*******x 的大作中提到】
: 谢谢。
: 这个问题我再想一想。
: forum上的讨论我看了,还得想一想。
: 我看了一点generic tutorial,觉得好像搞得太复杂了,没看进去。

T*******x
发帖数: 8565
6
Object obj = new Object();
Integer integer = (Integer) obj;
String string = (String) obj;
HashMap map = (HashMap) obj;
HashMap stringMap = (HashMap) obj;
以上的4个cast,只有最后一个给出warning,为什么?

downcast.

【在 w*******g 的大作中提到】
: whenever you have downcast in Java, it will have dynamic checks.
: If you use generic consistently, there are very few places you need downcast.

p***p
发帖数: 559
7
直接HashMap这个算什么构造函数呢,API里面没有
这样的啊
哪里有比较好的Totourin呢

http://forum.java.sun.com/thread.jspa?threadID=612574&messageID=3382719

【在 T*******x 的大作中提到】
: Object obj = new Object();
: Integer integer = (Integer) obj;
: String string = (String) obj;
: HashMap map = (HashMap) obj;
: HashMap stringMap = (HashMap) obj;
: 以上的4个cast,只有最后一个给出warning,为什么?
:
: downcast.

d****s
发帖数: 30
8

HashMap is not treated as a normal class.
I don't think Generic is mutual enough. C++'s template is
much better.
http://forum.java.sun.com/thread.jspa?threadID=612574&messageID=3382719

【在 T*******x 的大作中提到】
: Object obj = new Object();
: Integer integer = (Integer) obj;
: String string = (String) obj;
: HashMap map = (HashMap) obj;
: HashMap stringMap = (HashMap) obj;
: 以上的4个cast,只有最后一个给出warning,为什么?
:
: downcast.

w*******g
发帖数: 9932
9

because there is no way to check the last one dynamically when executing
that statement. Exception may arise later on.

【在 T*******x 的大作中提到】
: Object obj = new Object();
: Integer integer = (Integer) obj;
: String string = (String) obj;
: HashMap map = (HashMap) obj;
: HashMap stringMap = (HashMap) obj;
: 以上的4个cast,只有最后一个给出warning,为什么?
:
: downcast.

1 (共1页)
进入Java版参与讨论
相关主题
相关话题的讨论汇总
话题: string话题: hashmap话题: object话题: generic话题: java