aspose file tools*
The moose likes Beginning Java and the fly likes array being overwritten Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array being overwritten" Watch "array being overwritten" New topic
Author

array being overwritten

James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
I have a program with three classes the first `Employee class` introduces a hashIndex calculated from empName the second `EmployeeSort` takes the hashIndex and places the employee class instance into an array of which will be eventually be Employee class objects.I have the Last class passing the name and job using the JOptionPane unfortunately each pass overwrites the last entry in EmployeeSort .Could anyone point me to what I am doing wrong in the program.Heres the code


thankyou for your input in advance
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Whoa!!! my eyes are cross-eyed after reading that code. What are you trying to do? I don't understand what you are trying to do here, but I think problem might be in this code



You are creating a new EmployeeSort in each iteration of the loops. Every time the loop finishes one iteration the empSort1 object goes out of scope \, and a new one is created whenver you do "new EmployeeSort". In each iteration EmployeeSort has only one employee's information. So, when the loop finishes the last EmployeeSort will have the last employees information. Is that your problem?
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
correct ,the aim of the program is to end up with an array of Employee objects in employeeSort ,sorted according to hashIndex == element
if the element == full then hashIndex +1.the final outcome requires an output from the mainProgram
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Then you need to have just one EmployeeSort object created outside the loop. Inside the loop you need to get the Employee objects to the one and only EmployeeSort object.

I'm not entirely sure what your setArray function does, but my guess is that it is suppossed to sort the employees. I think you need to clean up the code around there too. It is rather convuluted. It looks like you are trying to store the empolyee at the hashIndex, and in case of collision you move to the next index. There are much cleaner ways of doing that
 
jQuery in Action, 2nd edition
 
subject: array being overwritten