my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Array losing info in loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array losing info in loop" Watch "Array losing info in loop" New topic
Author

Array losing info in loop

James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
I am trying to fill an array with String objects using a for loop.Thestring objects are assigned a hash code(has been change to int value).The hash has to to be matched with an element within an array When I pass this hash through a set of if statements to check it`s validity and save it to one of the elements within the array when the loop completes its first iteration it loses the info saved and I end up just saving one String each Iteration.I wonder if someone could shed some light on how I overcome this


thankyou for your input in advance
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Please post the code so we have a chance to point out the problem. Put your code between [ CODE ] ... [ /CODE ] tags (remove the spaces inside the brackets) to preserve formatting.
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
This is not the finnished article as the validity test only checks for matches within the for loop at the moment .It works from another class where the has is worked out but I have added a main method at the bottom to show what should be happening.Here`s the code
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
In the loop inside main(), you create a new Storage class for each iteration. Since each Storage has its own hashArray, each will store a single name_course in a single slot.

Instead, create a single Storage instance before the loop and store each name/course pair in the same Storage using a different slot. Given that the slot choice is random, I'm not sure how you'll go about doing it.

Also, since Storage is going to store name/course pairs, it probably shouldn't have name and course instance variables itself. I recommend creating a different class to represent a student's information (name and course).

Is the goal for this assignment to create your own hash table? If so, note that in a hash table, many objects may hash to the same slot.
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
thanks david,
The main main method in this class is not the one that I m using to store the values.The values are called from another class . Name and Course have been concanated and the hash is not random.It is generated in another class from a hashing algorithm using the name object.Just couldn`t figure out why I was getting individual entries.
thanks again for the help
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Okay, but does the other code you're using create a single or multiple Storage objects? If multiple, this is the problem.
 
GeeCON Prague 2014
 
subject: Array losing info in loop