我很困惑如何调用一个在同一个类中返回Key的方法,但是我想实现一个Hashtable。所以使用Java.Lang.Object从方法hashrealCode(Employee e)
保存密钥的Hashtable。这是不可能的吗?
public class Employee { // I created this class to prepare the Object
int id;
String name;
public Empployee(int id, String name) {
this.id = id;
this.name = name;
}
public int hashCode() {
final int Number = 31;
int result = 1;
result = Number * result + id;
result = Number * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public int id() {
return id;
}
public void setId(int Id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在此课程中,我想将密钥添加到我的Hashtable(数组)
public class HashTableEmployee {
Employee[] array;
public void addTheEmployee(Employee e){
for(int i = 0; i < array.length; i++) {
array[i] = hashrealCode(Employee e) //??? I just want to save the key form Method-hashrealCode(Employee e) to my array, but it didnt work
}
}
public Employee[] getArray(){
return array;
}
public void setArray(Employee[] array){
this.array = array;
}
public int hashrealCode(Employee e){ //create my own key
int hash_value = Math.abs(e.hashCode()) % array.length;
return hash_value; //I want to save this hashCode in my array
}
public Employee(int capacity){ // for the length of Array
array = new Employee[capacity];
}
}
那么我怎么不能将键(Method-hashrealCode)保存到我创建的方法中?当我试图调用该方法时,它无法正常工作