摘自http://www.cxybl.com/html/suanfa/201110125445.html

有改动

public class MyHashtable {
//表中元素个数
private int manyItems; private Object[] keys;
private Object[] data; //若索引i处存在元素,则hasBeenUsed[i]为true,否则为false
private boolean[] hasBeenUsed; //构造函数
public MyHashtable(int capacity){
if(capacity <= 0){
throw new IllegalArgumentException("capacity is negative!");
}
keys = new Object[capacity];
data = new Object[capacity];
hasBeenUsed = new boolean[capacity];
} //hash函数
private int hash(Object key){
return Math.abs(key.hashCode()%data.length);
} //当前索引发生冲突,找下一索引
private int nextIndex(int i){
if(i + 1 == data.length){
return 0;
}else{
return i + 1;
}
} /*
* 如果在表中找到指定的关键字,返回值为关键字的索引,否则返回-1
* 也就是说如果已经存在某个键时,返回的是该键的索引,如果没有这个键,则返回-1
* 在插入新的键时都返回-1
*/
private int findIndex(Object key){
int count = 0;
int i = hash(key);
System.out.println(i);
while((count<data.length)&&hasBeenUsed[i]){
if(key.equals(keys[i])){
return i;
}else{
count++;
i = nextIndex(i);
}
}
return -1; } /*
* 获取某个元素
*/
public Object get(Object key){
int index = findIndex(key);
if(index == -1){
return null;
}
else{
return data[index];
}
} /*
* 放入某个元素
*/
public Object put(Object key,Object element){
int index = findIndex(key);
Object answer; //判断索引是不是-1
if(index != -1){
answer = data[index];
data[index] = element;
return answer;
}else if(manyItems<data.length){
index = hash(key);
while(keys[index] != null){
index = nextIndex(index);
}
keys[index] = key;
data[index] = element;
hasBeenUsed[index] = true;
manyItems++;
return null; }else{
throw new IllegalStateException("Hsahtable is full");
}
} /*
* 删除某个元素
*/
public Object remove(Object key){
int index = findIndex(key);
Object answer = null;
if(index != -1){
answer = data[index];
data[index] = null;
keys[index] = null;
manyItems--;
}
return answer;
} /*
* 是否包含某键
*/
public boolean contains(Object key){
return (findIndex(key)!=-1);
} public static void main(String[] args) {
// TODO Auto-generated method stub
MyHashtable table = new MyHashtable(3); table.put(1, "中国");
Object ob1 = table.put(2, "美国");
table.put(3, "安徽"); System.out.println(table.get(2).toString());
System.out.println(ob1);
} }

最新文章

  1. JS学习总结(新手)
  2. jquery on和bind
  3. Win10如何隐藏Windows Defender任务栏图标
  4. DbUtils使用时抛出Cannot get a connection
  5. QMessageBox 弹出框上的按钮设置为中文
  6. get started with laravel
  7. Asp.net主题(theme)和皮肤(skin)的使用
  8. yii开启gii功能
  9. HDU 4405 Aeroplane chess(期望)
  10. Git命令行连Github与TortoiseGit 连Github区别
  11. OpenStack_I版 3.glance部署
  12. iOS------Xcode 的clang 扫描器可以检测出所有的内存泄露吗
  13. vmware Harbor 复制功能试用
  14. mybatis02--增删改查
  15. Linux中运行SpringBoot项目,永久运行
  16. &lt;构建之法&gt;13——17章的读后感
  17. android-------- 常用且应该学习的框架
  18. poj 2485 Highways (最小生成树)
  19. 推荐的bootstrap之 formgroup表单布局样式
  20. HDU 6153 A Secret(扩展kmp)

热门文章

  1. Linux查看外网IP
  2. 中美贸易战再次开启,世界两极化进程正在加快形成!..... Copyright: 1688澳洲新闻网 Read more at: https://www.1688.com.au/world/international/2018/06/17/369368/
  3. sqlite3简单教程整理
  4. bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
  5. kmplayer音轨切换(换配音)
  6. python第二篇:windows 下virtualenvwrapper虚拟环境搭建
  7. Array对象(一)
  8. 分享知识-快乐自己:idea的断点调试
  9. svn_学习_01_TortoiseSVN使用教程
  10. QWidget上下文菜单处理函数