This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am working on a program that will read input from a Excel file
The input will be placed into a object that I created called Entity.
The object will then be placed into a a arraylist;
I Run the program and it creates the correct number of Entity objects in the arraylist.
The objects are created and the correct information is being placed inside them.
However the objects in the arraylist are being overwritten by the last object added.
How do I stop from overwriting all the objects in the list ??
I'm new at this but I wonder if display tags would be useful for you. In our shop, when we're dealing with displaying arrays of information, we use display tags which handling paging, sorting, links and a lot of other stuff I've yet to learn. Just go to http://displaytag.sourceforge.net/11/index.html and look at some examples.
This is different than the code you posted in your first post, and the difference is your problem.
You make on instance of Entity named s. You fill it, you put it in the ArrayList. You keep the same instance of Entity, you change its values, and you put it into the ArrayList again. You then repeat...
So what have at the end is 1 Entity, placed into ArrayList several times. Since there is only one Entity created, then all the entries in the ArrayList have the same values (they are, after all, the same Object).
Why did you comment out the s = new Entry(); line? It wasn't commented in the original post. Uncomment that code and your problem will be solved. In this scenario, s is assigned a New Entity instance each round through the loop. This new instance is filled, and added to the ArrayList. Which means each entry in the ArrayList is a different Object, and so can have different values.
Joined: May 01, 2009
Thanks for the input.
I have been trying different variations of code to try to fix my problem.
I Commented out the line due to it had no affect on my output.
Meaning that the Entity object overwrote the other objects in the arraylist even with the s = new Entity(); commented or uncommented out.
I appreciate any helpful advice.
I am copying and pasting my output with the s = new Entity(); line uncommented.
Number of Sheets in Workbook : 32
Scenario time is : 24
This sheet is SimpleProviderConsumables
The number of rows in this sheet are : 47
The number of Entities in this scenario is : 12
1) Are you using any static variables in the Entity?
2) Are you sure you are getting different values in from the source? Print out the values your are storing inside the Loop to make sure they change.
Joined: May 01, 2009
I implemented the entity generator method and my program works..