Redian新闻
>
Java大侠们:Hashtable help please!
avatar
Java大侠们:Hashtable help please!# Java - 爪哇娇娃
l*r
1
是不是作为key存入的和取出使得variable不一样,就不行呀。
如果key时String,怎么才能改equals()的method呀?还是有其他办法?
各位大侠指教指教吧!
avatar
xt
2

How did you do that?
what do you mean?

【在 l*r 的大作中提到】
: 是不是作为key存入的和取出使得variable不一样,就不行呀。
: 如果key时String,怎么才能改equals()的method呀?还是有其他办法?
: 各位大侠指教指教吧!

avatar
KG
3
看不懂

【在 l*r 的大作中提到】
: 是不是作为key存入的和取出使得variable不一样,就不行呀。
: 如果key时String,怎么才能改equals()的method呀?还是有其他办法?
: 各位大侠指教指教吧!

avatar
l*u
4

Inside a JVM, Java is "pass-by-reference". The key stored in Hashtable should
be the SAME INSTANCE as the one you try to retrieve. The only exception is
String and Wrapper which have literal pools. For example, String a = "a";
String b = "a"; (a==b gives you true!), while String c = new String("a"), c is
not the same instance of a & b (c==a returns false).

【在 l*r 的大作中提到】
: 是不是作为key存入的和取出使得variable不一样,就不行呀。
: 如果key时String,怎么才能改equals()的method呀?还是有其他办法?
: 各位大侠指教指教吧!

avatar
l*r
5
I am not a Java expert, so please bear with me for my silly question--
when constructing hashtable, I passed a String variable to the hashtable as
the Key in order to retrieve its value later on. But when retrieving, I am
using another String variable -- although the value of the string is same. The
hashtable returns no value.
For example, for the following table
Id value nId
1x a 3x
2x b 3x
3x c 7x
the variable ID is the

【在 KG 的大作中提到】
: 看不懂
avatar
l*u
6

When you define a new String, avoid to call new String() constructor, try all
effort to use literal pool, like this, String a = "my new string";

【在 l*r 的大作中提到】
: I am not a Java expert, so please bear with me for my silly question--
: when constructing hashtable, I passed a String variable to the hashtable as
: the Key in order to retrieve its value later on. But when retrieving, I am
: using another String variable -- although the value of the string is same. The
: hashtable returns no value.
: For example, for the following table
: Id value nId
: 1x a 3x
: 2x b 3x
: 3x c 7x

avatar
t*s
7
if both Id and nId are String variable,
then use Id.equal(nID) to judge whether nId's content is the same as Id.
Note: don't use Id == nId to do such thing

【在 l*r 的大作中提到】
: I am not a Java expert, so please bear with me for my silly question--
: when constructing hashtable, I passed a String variable to the hashtable as
: the Key in order to retrieve its value later on. But when retrieving, I am
: using another String variable -- although the value of the string is same. The
: hashtable returns no value.
: For example, for the following table
: Id value nId
: 1x a 3x
: 2x b 3x
: 3x c 7x

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。