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燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP