Java实现的散列表

Java实现的散列表,如下代码: public class MyHashtable{ private int manyItems; //表中元素个数 private Object[]keys; private Object[]data; private boolean []hasBeenUsed; //若索引i处存在元素,则hasBeenUsed[i]为ture,否则为false public MyHasht

Java实现的散列表,如下代码:

 
 
  1. public class MyHashtable { 
  2.      
  3.     private int manyItems;//表中元素个数 
  4.     private Object[] keys; 
  5.     private Object[] data; 
  6.     private boolean[] hasBeenUsed;//若索引i处存在元素,则hasBeenUsed[i]为ture,否则为false 
  7.      
  8.     public MyHashtable(int capacity) { 
  9.         if(capacity <= 0
  10.             throw new IllegalArgumentException("capacity is negative"); 
  11.          
  12.         keys = new Object[capacity]; 
  13.         data = new Object[capacity]; 
  14.         hasBeenUsed = new boolean[capacity]; 
  15.     } 
  16.      
  17.     //hash函数 
  18.     private int hash(Object key) { 
  19.         return Math.abs(key.hashCode()%data.length); 
  20.     } 
  21.      
  22.     //当前索引发生冲突,找下一索引 
  23.     private int nextIndex(int i) { 
  24.         if(i + 1 == data.length) { 
  25.             return 0
  26.         } else { 
  27.             return i + 1
  28.         } 
  29.     } 
  30.      
  31.     //如果在表中找到指定的关键字,返回值为关键字的索引,否则返回-1 
  32.     private int findIndex(Object key) { 
  33.         int count = 0
  34.         int i = hash(key); 
  35.          
  36.         while((count < data.length) && hasBeenUsed[i]) { 
  37.             if(key.equals(keys[i])) { 
  38.                 return i; 
  39.             } else { 
  40.                 count++; 
  41.                 i = nextIndex(i); 
  42.             } 
  43.         } 
  44.         return -1
  45.     } 
  46.      
  47.     public Object get(Object key) { 
  48.         int index = findIndex(key); 
  49.          
  50.         if(index == -1) { 
  51.             return null
  52.         } else { 
  53.             return data[index]; 
  54.         } 
  55.     } 
  56.      
  57.     public Object put(Object key, Object element) { 
  58.         int index = findIndex(key); 
  59.         Object answer; 
  60.          
  61.         if(index != -1) { 
  62.             answer = data[index]; 
  63.             data[index] = element; 
  64.             return answer; 
  65.         } else if(manyItems < data.length) { 
  66.             index = hash(key); 
  67.             while(keys[index] != null) { 
  68.                 index = nextIndex(index); 
  69.             } 
  70.             keys[index] = key; 
  71.             data[index] = element; 
  72.             hasBeenUsed[index] = true
  73.             manyItems++; 
  74.             return null
  75.         } else { 
  76.             throw new IllegalStateException("Hashtable is full!"); 
  77.         } 
  78.     } 
  79.      
  80.     public Object remove(Object key) { 
  81.         int index = findIndex(key); 
  82.         Object answer = null
  83.          
  84.         if(index != -1) { 
  85.             answer = data[index]; 
  86.             data[index] = null
  87.             keys[index] = null
  88.             manyItems--; 
  89.         } 
  90.         return answer; 
  91.     } 
  92.      
  93.     public boolean contains (Object key) { 
  94.         return (findIndex(key) != -1); 
  95.     } 
  96.      
  97.     public static void main(String[] args) { 
  98.         MyHashtable table = new MyHashtable(100); 
  99.         table.put(1"china"); 
  100.         table.put(2"美国"); 
  101.         System.out.println(table.get(2).toString()); 
  102.     } 
  103. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值