由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请教一个语法和递归的问题
相关主题
Object比较请教一段代码,关于hashCode()
Re: 谁有Java或Oracle的毒招 ?问个set和literal String的问题
Do I need to implement equals and hashCode in domain object也问个 HashMap问题
问一道关于Vector的题问题征解
Java的工作面试主要考什么?主要是算法吗?问个基本问题
问个Object.hashCode()的问题有熟悉Java Reflection的吗
问个hashtable实现问题一道java题
问HashSet的问题?java positions
相关话题的讨论汇总
话题: prefix话题: hashcode话题: equals话题: int话题: pref
进入Java版参与讨论
1 (共1页)
j***y
发帖数: 2074
1
在The Practice of Programming(中文名:《编程实践》)的第74~75页上看到下面的
代码:
static final int MULTIPLIER = 31; // for hashCodeO
// Prefix hashCode: generate hash from all prefix words
public int hashCode()
{
int h = 0;
for (int i = 0; i < pref.sizeO; i++)
h = MULTIPLIER * h + pref.elementAt(i).hashCodeO;
return h;
}
// Prefix equals: compare two prefixes for equal words
public boolean equals(Object o)
{
Prefix p = (Prefix) o;
for (int i = 0; i < pref.sizeO; i++)
if (!pref.elementAt(i).equals(p.pref.elementAt(i)))
return false;
return true;
}
这两个method里面的pref都是个vector,里面装的是string。这两个method貌似都是递
归函数。
我的问题是,hashCode()和equals()都是class Prefix(见下面的代码)的method,但
从调用方式看,怎么又成了string的成员函数呢?不解啊。
class Prefix {
public Vector pref; // NPREF adjacent words from input
// Prefix constructor: duplicate existing prefix
Prefix(Prefix p)
{
pref = (Vector) p.pref.clone();
}
// Prefix constructor: n copies of str
Prefix(int n, String str)
{
pref = new VectorO;
for (int i = 0; i < n; i++)
pref.addElement(str);
}
//下面应该是hashCode()和equals()的声明
}
请高手答疑,谢了先。
r*****l
发帖数: 2859
2
equals and hashCode are defined in Object class. Every Java class extends
Object, thus every class has equals and hashCode.
In your example, there is no recursive call.

【在 j***y 的大作中提到】
: 在The Practice of Programming(中文名:《编程实践》)的第74~75页上看到下面的
: 代码:
: static final int MULTIPLIER = 31; // for hashCodeO
: // Prefix hashCode: generate hash from all prefix words
: public int hashCode()
: {
: int h = 0;
: for (int i = 0; i < pref.sizeO; i++)
: h = MULTIPLIER * h + pref.elementAt(i).hashCodeO;
: return h;

x*****p
发帖数: 1707
3
Agree with the upper level.
j***y
发帖数: 2074
4
谢谢大家啊,是我搞错了。之前还以为这两个函数都是递归调用呢。
1 (共1页)
进入Java版参与讨论
相关主题
java positionsJava的工作面试主要考什么?主要是算法吗?
answer Re: how HashMap/Hashtable compare key?问个Object.hashCode()的问题
[转载] How do I sort a map in Java?问个hashtable实现问题
大家写java class的时候是完全封装的么?问HashSet的问题?
Object比较请教一段代码,关于hashCode()
Re: 谁有Java或Oracle的毒招 ?问个set和literal String的问题
Do I need to implement equals and hashCode in domain object也问个 HashMap问题
问一道关于Vector的题问题征解
相关话题的讨论汇总
话题: prefix话题: hashcode话题: equals话题: int话题: pref