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'm a newbie in java, and am struggling with something stupid with a collection (see the simplified below)
The Collection contains a DTO (int bierNr, int Aantal)
the intention is to use the DTO for every new item in an ArrayList, but somehow if i run through the collection, the only value I see, is the last one I put in,
where it should normally give diff values.
I know i'm looking over something, but I just cant find out what :-)
Keep in mind that the collection does not make a copy of your objects, it just keeps a reference to it... So...
This creates a new DTO instance, configures it, and places it in the collection. And this...
Reconfigures your DTO object (the one that is also referred to by the collection), and places it (again) into the collection. So, you collection has two identical references to the same DTO object, which has been configured to the last reconfigure.
bestellijn = new BestellijnDTO();
public void legProductInMandje(int bierNr, int aantal)
This is the code that works:
Just to check weither i get what i'm doing :-)
First mistake in the code didnt work, because the constructor didnt start the Collection, I never initialized it.
Second mistake only use the DTO in the methods where i need it and dont put them in the constructor in this case
Since i'm only working with two integers, is it smart to put them in an ArrayList or would you prefer a HashMap with biernr as key and aantal as value???
Joined: Apr 30, 2009
Campbell Ritchie wrote:And welcome to JavaRanch
thank you :-)
Joined: Apr 30, 2009
got another question too :
i'm making a 3 TIER application (MVC)
BierOnlineShop (containing sevlets, listener, ...)
BierOnlineShopAppl(Data layer and BusinessLayer)
BierOnlineShopGem(DTO's and Exceptions)
Ofcourse I need to use the basket in the application, and am planning to use an HttpSessionBinderListener for it (or would you do it otherwise??).
Correct me if i'm wrong in my thinking, but since the listeners are in my webapplication, i need to make the HttpSessionBinderListener in the listener package and let it then initiate an instance of the basket. once the session starts.