HashCode,尝试获取单词或数字的最后几个字母
我有这个 hashCode 函数,我一直试图用 Eclipse 实现它,但我的测试一直失败。我正在尝试编写一个哈希码函数,该函数返回单词或数字的最后 2 或 3 个字符。这是我当前的代码:
public int hashCode(String value){
String test = value;
int hash = 1;
int len = test.length();
System.out.println(len);
for ( int i=0; i<len; i++ )
{
hash = 31 * hash + len;
}
return hash;
}
这是我的 Junit 测试:
@Test // hashCode()
public void testHashCode1() {
//Integer key1 = 123;
String key2 = "and";
assertEquals("and", key2.hashCode());
}
每次运行测试时,它都会失败,指示:
预期但为:<96727>
现在我真的想让这个函数接受一个单词或数字并返回所接受对象的最后两个或三个字符。有谁知道如何使用 hashCode 来做到这一点。任何帮助将不胜感激
I have this hashCode function that I have been trying to implement with Eclipse but my Tests keep failing. I am trying to write a Hashcode function that returns that last 2 or 3 chars of a word or number. Here is my current code:
public int hashCode(String value){
String test = value;
int hash = 1;
int len = test.length();
System.out.println(len);
for ( int i=0; i<len; i++ )
{
hash = 31 * hash + len;
}
return hash;
}
Here is my Junit Test:
@Test // hashCode()
public void testHashCode1() {
//Integer key1 = 123;
String key2 = "and";
assertEquals("and", key2.hashCode());
}
Everytime I run my test it fails indicating:
expected but was: <96727>
Now I really would like for this function to take in either a Word or Number and return the last two or three chars of the object taken in. Does anyone know how to do this using hashCode. Any help would be most appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
public int hashCode(String value)
并调用
assertEquals("and", key2.hashCode());
您甚至没有正确地覆盖它,请更改签名以不接受任何参数:)
public int hashCode(String value)
and calling as
assertEquals("and", key2.hashCode());
you are not even overriding it properly change the signature to take no arguments please :)
这可能是编辑器破坏代码的问题,但我没有看到循环内任何地方使用
value
或test
。It may be a problem with the editor mangling the code, but I do not see
value
ortest
used anywhere inside the loop.