📄 myhash.java
字号:
/**
A class for representing the hashtable built with the linked list
Written by : H Yu
*/
public class MyHash {
//class constants
public static final int DefaultSIZE = 97;
//instance variables
MyLinkedList[] hashtable;
int tablesize;
//constructors
/**
*creat an instance of class MyHash with default tablesize
*/
public MyHash() {
tablesize = DefaultSIZE;
hashtable = new MyLinkedList[tablesize];
for (int i=0;i<tablesize;i++) {
hashtable[i] = new MyLinkedList() ;
}
}
/**
*creat an instance of class MyHash with specific parameters
*@param s size of hashtable
*/
public MyHash (int s) {
tablesize = s;
hashtable = new MyLinkedList[tablesize];
for (int i=0;i<tablesize;i++) {
hashtable[i] = new MyLinkedList();
}
}
//mutator method
/**
*insert a word to the hashtable
*@param w word
*/
public void setWord(String w) {
int index = MyHashFunction.HashFunction(w,tablesize);
hashtable[index].setWord(w);
}
//accessor methods
/**
*return true if a word exist in the hashtable
*@param w word
*/
public boolean hasWord(String w) {
boolean exist=false;
for(int i=0;i<tablesize;i++) {
if(hashtable[i].isWord(w)) {
exist=true;
}
}
return(exist);
}
/**
*return count if w matches to the word in the table
*return 0 if not
*@param w word
*/
public int getCount(String w) {
int count = 0;
for(int i=0;i<tablesize;i++) {
if(hashtable[i].isWord(w)) {
count = hashtable[i].getCount(w);
}
}
return(count);
}
/**
*return the index if w matches the word in the list
*return 100+tablesize if w does not exist in the hashtable
*@param w word
*/
public int getIndex(String w) {
int index = tablesize+100;
for(int i=0;i<tablesize;i++) {
if(hashtable[i].isWord(w)) {
index = i;
}
}
return(index);
}
/**
*return the length of the list with index i
*@param i index
*/
public int getListLength(int i) {
int length = 0;
if(i<tablesize)
length = hashtable[i].getLength();
return(length);
}
/**
*return the total number of objects in the hashtable
*/
public int getNumber() {
int number = 0;
for(int i=0;i<tablesize;i++) {
if(hashtable[i].getWord(0) != null)
number += hashtable[i].getLength();
}
return(number);
}
/**
*return the word with the index i and j
*@param i index of hashtable
*@param j index of list
*/
public String getWord(int i,int j) {
String word = null;
if(i<tablesize && j <= hashtable[i].getLength()) {
word=hashtable[i].getWord(j);
}
return(word);
}
/**
*return the total number of objects in the hashtable including the dummies
*/
public int getTotalNumber() {
int number = 0;
for(int i=0;i<tablesize;i++) {
int l=hashtable[i].getLength();
number += l+1;
}
return(number);
}
/**
*return the hashtable size
*/
public int getSize() {
return(tablesize);
}
//print method
public void PrintHashTable() {
for(int i=0;i<tablesize;i++) {
System.out.println(hashtable[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -