> I have a really strange problem:
> I fill a Hashtable using an own Object as key:
There are actually a couple of problems here. One you've seen, whereas
the other just hasn't happened to bite you yet. First,
> public NameTimeshift(String[] name, int[] timeShift){
> this.name = new String[name.length];
> this.name = name;
> this.timeShift = new int[timeShift.length];
> this.timeShift=timeShift;
> }
This is ultimately the problem. When you use this constructor to create
a new NameTimeshift, it will share the same String[] and int[] that you
passed in. My guess is that you're passing in the same String[] and
int[] to create each object, so your objects are all nominally
different, but they share the same arrays so they look exactly the same.
When you write:
> this.name = name;
that discards the previous value of name (which you just assigned) and
replaces it with a pointer to the same object pointed to by the
parameter called name. You probably wanted to copy the contents
instead. That looks like this:
System.arraycopy(name, 0, this.name, 0, name.length);
Do the same for timeShift as well.
The other problem is:
> public boolean equals(Object o){
> NameTimeshift nt = (NameTimeshift)o;
> if(nt.hashCode()==this.hashCode()) return true;
> return false;
> }
This is a misuse of hashCode. Hash codes are NOT unique between
different objects. They can't be, because a hash code is only 32 bits
long, whereas the number of possible object states in a String is
something like 2^2^16 (about 1 followed by 25000 zeros). The number of
possible states of a String[] and an int[] is far, far greater.
Hash codes provide a first approximation at equality comparison. You
then need to compare the fields themselves to see if they are equal.
That involves using a couple loops to walk through all the elements.
Hope that helps,

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
feju2000@gmx.de - 17 Jun 2006 22:53 GMT
Hi Chris,
thank you very much! The equals-method was not the problem or (better
said) did not had to be changed. But the hint to use System.arraycopy
was really useful.
Greetings,
Felix
Chris Smith schrieb:
> Hope that helps,
>
> --
> Chris Smith - Lead Software Developer / Technical Trainer
> MindIQ Corporation
Patricia Shanahan - 18 Jun 2006 06:29 GMT
> Hi Chris,
> thank you very much! The equals-method was not the problem or (better
> said) did not had to be changed. But the hint to use System.arraycopy
> was really useful.
The equals method issue is a bug, but one that will cause problems
very intermittently, depending on how many items you put in your Hashtable.
Patricia
Chris Smith - 18 Jun 2006 06:45 GMT
> thank you very much! The equals-method was not the problem or (better
> said) did not had to be changed. But the hint to use System.arraycopy
> was really useful.
As I said before, that's a bug looming in your code that hasn't happened
to bite you yet. It's still wrong, and it will likely cause a
spectacular failure one day unless you fix it. Just because you haven't
seen its effects yet doesn't mean you can safely ignore it.
It's up to you, of course... I just hope you're not writing anything of
importance.

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
Twisted - 18 Jun 2006 16:30 GMT
The equals() method given has another bug -- it's prone to throwing
ClassCastExceptions, which you may not want. Prepending the following
two lines to the method body will fix that, as well as speed it up in
certain cases:
if (this == o) return true;
if (!(o instanceof NameTimeshift)) return false;
Chris Smith - 18 Jun 2006 17:08 GMT
> The equals() method given has another bug -- it's prone to throwing
> ClassCastExceptions, which you may not want. Prepending the following
[quoted text clipped - 3 lines]
> if (this == o) return true;
> if (!(o instanceof NameTimeshift)) return false;
Good catch!

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
feju2000@gmx.de - 19 Jun 2006 14:15 GMT
Okay,
I'll fix both mentioned problems!
Thanks again...
Felix
Chris Smith schrieb:
> > The equals() method given has another bug -- it's prone to throwing
> > ClassCastExceptions, which you may not want. Prepending the following
[quoted text clipped - 9 lines]
> Chris Smith - Lead Software Developer / Technical Trainer
> MindIQ Corporation
Twisted - 19 Jun 2006 20:08 GMT
> > The equals() method given has another bug -- it's prone to throwing
> > ClassCastExceptions, which you may not want. Prepending the following
[quoted text clipped - 5 lines]
>
> Good catch!
Nah, just standard practise in writing equals() methods in my neck of
the woods. :)