作者miyabichan (miyabichan)
看板java
标题[问题] 找得到内容却print不出来
时间Thu Nov 28 00:04:57 2013
这个作业我们要自己实作只能存正整数 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