This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

my add method  RSS feed

 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i am trying to add to an array where the index of the array == hash code i have generated if this element is already taken move to the next empty space and if the end of the array is reached start from the begining.

at the moment all i have is:

for(int i = 0 i<list.length; i++)
while( (i<list.length) && (!found)
{
if(list[i]==hash;
found = true;
else
i++;
}

while this finds the match for the index and hash code i need to move on if this is already occupied to the next available space and carry on searching
if the end of the array is reached.

i have been at this for 2 days and i just can not get it right
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create the array, each element will be initialized to null. So to find the first "empty" index after i, you should be able to use something like...

(If the array contains primitives rather than objects, then test for a zero value rather than null.)
[ June 28, 2005: Message edited by: marc weber ]
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi marc
that seem simple enough although what needs to happen is when the hash code is found i have to try to match the hashCode to the index number if that element is full move to next available element and if the end of the array is reached carry on searching.

so i have to 1,check to see if they are equal 2, check to see if it is empty
3, if not carry on.

i can match the index and hash but after that it all goes down the pan!!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the hashCode supposed to equal the index (i == hash)? Or is the hashCode supposed to equal what's in the array at that index (list[i] == hash)?

The way I'm reading this, you have some object with a hashCode and you want to put that object into an array. Ideally, you want to put it in the array at the index that equals the hashCode. But if that position of the array already contains something, then you want to put the object in the next "empty" spot. If you reach the end of the array and still haven't found an empty spot, then you start looking for an empty spot at the beginning of the array.

Is that accurate?
[ June 28, 2005: Message edited by: marc weber ]
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi marc,
yes your spot on that's it exactly what i am trying to do.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so you have someObject with a hashCode. Ideally, you want to put it here...

But first you need to make sure that list[index] isn't already occupied. If the reference at that index is not null, then it's occupied. So while it is not null, you need to increment the index...

But you also want to make sure that you don't increment past the array bound. If you reach that point, then you want to reset index to zero so you can start at the beginning of the array. So instead of just incrementing, you might want to consider something like this...

See what you can do with that...
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, marc
realy appreciate that, i will have a go with that and see how that works for me. i will post the result.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi marc,
thank you i now have a working soultion for that method i was putting the add obj part, "list[hash] = studentObj;" in an else part of an if statement and then it wan not using the icremented value. Thus overwriting what was already there!!! ahhh, still i am only at the early stage of java so maybe i'm not doing so bad!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!