上代码。
写点小东西自娱自乐了。
在我理解,hash的实现既是利用hash算法实现的一种特殊的数据转化,原数据与转化过后的数据通过hash算法想关联。通过key值在存储结构中检索数据。
存储结构,我的理解是数组加链表。数组通过key的关联存储链表的头。数据检索时,通过key在数组中检索到相应链表的位置,然后在链表中检索到数据。
/**
* hash表的基本创建
* 测试入口实例化
*/
public class CreateHash {
//定义hash根数组
private Node hashList[];
public CreateHash(int size2) {
Create(size2);
}
/**
* hash表创建
*/
public void Create(int size){
hashList=new Node[size];
}
/**
* 添加元素
* @param obj
*/
public void Add(Object obj){
int index=HashCode(obj);
if(hashList[index]==null){
hashList[index]=new Node(obj);
}else {
Node node =new Node(obj);
node.setNext(hashList[index]);
hashList[index]=node;
}
}
/**
* 打印程序
*/
public void PrintAll(){
for(int i=0;i<hashList.length;i++){
int count=1;
Node tempNode=hashList[i];
while(tempNode!=null){
System.out.println("第"+i+"行"+"第"+count+"个是:"+tempNode.getObj());
tempNode=tempNode.next;
}
System.out.println("本行打印完毕");
}
System.out.println("数据打印完毕");
}
/**
* hash算法
* @param obj
* @return
*/
private int HashCode(Object obj) {
return obj.hashCode()%10;
}
}
/**
* 节点类
*/
public class Node {
public Object obj;
public Node next;
//构造器
public Node (Object obj){
this.obj=obj;
}
//节点数据返回
public Object getObj(){
return obj;
}
//返回当前节点的下个节点
public Node getNext(){
return this.next;
}
//设置当前节点的下个节点
public void setNext(Node next){
this.next=next;
}
}
写点小东西自娱自乐了。
在我理解,hash的实现既是利用hash算法实现的一种特殊的数据转化,原数据与转化过后的数据通过hash算法想关联。通过key值在存储结构中检索数据。
存储结构,我的理解是数组加链表。数组通过key的关联存储链表的头。数据检索时,通过key在数组中检索到相应链表的位置,然后在链表中检索到数据。
967

被折叠的 条评论
为什么被折叠?



