java 板


LINE

这个作业我们要自己实作只能存正整数 integer 的一个 chained hash table, 然後写四个 Method 分别是 insert, delete, lookup 跟 print.. 我的问题在 print 这个部份。 资料结构我选择用 Hashmap, 用 Hash function 算出来的杂凑数存成 Key, Value 则用 LinkedList.. 测试的时候 insert 跟 lookup 都没问题, 但我要把他 print 出来的时候 却说所有 Hashmap 的 value (= LinkedList) 是 null.. 我想了很久找不出盲点在哪里,可不可以请厉害的众板友指点我一下? 先谢过了! (以下程式码我就略过 main Method..) public class HashTable { private int a, n, m; private Map<Long, LinkedList<Integer>> ht = new HashMap<Long, LinkedList<Integer>>(); /** * Constructor * @param a, n, m: the parameters for hash function: * h(x) = (a*x mod n) mod m. * and the hashtable should have size of m */ public HashTable(int a, int n, int m){ this.a = a; this.n = n; this.m = m; for(long i = 0; i < m; i++){ ht.put(i, new LinkedList<Integer>()); } } /** * @return ture if x positiv, otherwise false. */ public boolean positiv(int x){ return (x >= 0); } /** * @param value: the object to be inserted in the hashtable * insert the number in the hash table, * index calculate through the hash function given. */ public void insert(int value){ if(positiv(value)){ long key = (a * value % n) % m; ht.get(key).add(value); } } /** * @param value: the object to be found * @return true if the value in the hashtable */ public boolean lookup(int value){ if(!positiv(value)){ return false; } long key = ((a * value) % n) % m; return ht.get(key).contains(value); } /** * @param value: the object to be deleted * Removes the (first) occurrence of the specified element * from this hashtable, if it is present */ public void delete(int value){ if(positiv(value)){ long key = ((a * value) % n) % m; ht.get(key).remove((Object) value); // if not with Object typecasting, // program will "see" the value (int) as an index // (better solution ?) } } /** * print out the hashtable */ public void print(){ for(int key = 0; key < m; key++){ System.out.print(key + " "); // output index if(ht.get(key) != null){ for(int i = 0; i < ht.get(key).size(); i++){ System.out.print(ht.get(key).get(i) + " "); } } System.out.println(); } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 134.93.102.77
1F:推 PsMonkey:为什麽突然变成 int 11/28 00:16
2F:→ miyabichan:啊对不起, 现在才看到..因为写完以後才看到作业要求 11/28 00:18
3F:→ miyabichan:index 用 long, 结果没有全改到 orz 11/28 00:19
4F:→ miyabichan:谢谢,改完就没问题了,我是白痴 orz 11/28 00:21
5F:→ adrianshum:我觉得最诡异的是你拿一个hashtable来实作hashtable会 11/28 05:47
6F:→ adrianshum:不会有点多余?你不是应该用array 吗?... 11/28 05:47
用一般 array 我无法 dynamic 的改变 array 长度 一开始我也想用 array 但想破头都不觉得用 2-d array 的方式比 collection 好
7F:→ Aztecs:你都用API的了干嘛还自己写XDDD 11/28 09:45
8F:→ Killercat:作业咩 11/28 20:14
9F:→ miyabichan:我们要写一个 chained hash table..要是可以用现有的 11/28 20:44
10F:→ miyabichan:资料结构我也想 QQ 11/28 20:46
11F:→ sbrhsieh:你应该是搞错作业的要求了 11/28 21:22
12F:→ sbrhsieh:这样子的内容如果可被接受,这作业一点意义都没有~ 11/28 21:27
这堂课主要是 algorighm.. 我一直觉得要我们写程式是助教想要整死我们 这个礼拜又要自己写一个 skip list 程式出来了.. ※ 编辑: miyabichan 来自: 134.93.80.59 (11/30 22:26)
13F:→ dream1124:我想sb大的意思不是作业本身没意义, 是你的解法没意义 12/01 10:12
14F:→ Killercat:skip list算简单了 你到後面碰到一颗颗的树你会怀念他的 12/01 23:56
15F:→ Killercat:然後最後碰到KD树就开始生不如死(误) 12/01 23:57







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP