F
feju2000
Hi there,
I have a really strange problem:
I fill a Hashtable using an own Object as key:
package trainfile.dictionaries;
public class NameTimeshift {
//private String name="";
String[] name;
int[] timeShift;
public NameTimeshift(String name, int timeShift){
this.name = new String[]{name};
//this.id = id;
this.timeShift = new int[]{timeShift};
}
public NameTimeshift(String[] name, int[] timeShift){
this.name = new String[name.length];
this.name = name;
this.timeShift = new int[timeShift.length];
this.timeShift=timeShift;
}
public int getTimeShift(){
return timeShift[0];
}
public String getName(){
return name[0];
}
public String[] getFollowingName(){
return name;
}
public int[] getFollowingTimeshift(){
return timeShift;
}
public boolean equals(Object o){
NameTimeshift nt = (NameTimeshift)o;
if(nt.hashCode()==this.hashCode()) return true;
return false;
}
public int hashCode(){
/*char[] c = new char[name.toCharArray().length];
c = name.toCharArray();
int result=0;
for(int i = 0; i<c.length; i++){
new Character(c).hashCode();
}*/
//String hash = name.hashCode()+""+new Integer(timeShift).hashCode();
int result = 0;
for(int i = 0; i<name.length; i++){
result+=name.hashCode();
result+=new Integer(timeShift).hashCode();
}
//int hash = name.hashCode()+timeShift.hashCode();
return result;
}
}
The hashcode and equals methods work fine, because filling the
Hashtable works as mentioned.
But after filling the Hashtable and using the Enumeration from the
keys()-method, it seems like the Hashtable just has x-times the last
NameTimeshift-object as keys, where x is the count of Objects I put in
the Hashtable. Why does this problem appear?
Thanks,
Felix
I have a really strange problem:
I fill a Hashtable using an own Object as key:
package trainfile.dictionaries;
public class NameTimeshift {
//private String name="";
String[] name;
int[] timeShift;
public NameTimeshift(String name, int timeShift){
this.name = new String[]{name};
//this.id = id;
this.timeShift = new int[]{timeShift};
}
public NameTimeshift(String[] name, int[] timeShift){
this.name = new String[name.length];
this.name = name;
this.timeShift = new int[timeShift.length];
this.timeShift=timeShift;
}
public int getTimeShift(){
return timeShift[0];
}
public String getName(){
return name[0];
}
public String[] getFollowingName(){
return name;
}
public int[] getFollowingTimeshift(){
return timeShift;
}
public boolean equals(Object o){
NameTimeshift nt = (NameTimeshift)o;
if(nt.hashCode()==this.hashCode()) return true;
return false;
}
public int hashCode(){
/*char[] c = new char[name.toCharArray().length];
c = name.toCharArray();
int result=0;
for(int i = 0; i<c.length; i++){
new Character(c).hashCode();
}*/
//String hash = name.hashCode()+""+new Integer(timeShift).hashCode();
int result = 0;
for(int i = 0; i<name.length; i++){
result+=name.hashCode();
result+=new Integer(timeShift).hashCode();
}
//int hash = name.hashCode()+timeShift.hashCode();
return result;
}
}
The hashcode and equals methods work fine, because filling the
Hashtable works as mentioned.
But after filling the Hashtable and using the Enumeration from the
keys()-method, it seems like the Hashtable just has x-times the last
NameTimeshift-object as keys, where x is the count of Objects I put in
the Hashtable. Why does this problem appear?
Thanks,
Felix