It's not a secret anymore!*
The moose likes Servlets and the fly likes Collection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Collection problem" Watch "Collection problem" New topic
Author

Collection problem

mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
Hi everyone,

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 :-)

thanks for your help,

penny

OUTPUT





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
And welcome to JavaRanch
mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
thanks for the answer, it solved my problem,

below is the actual code i needed to use, but didnt work as i said yesterday.

i'm making a basket to put some items in, just basic nothing fancy

This is the code that didnt work :

public class MandjeFacade {

private Collection mandje;
private BestellijnDTO bestellijn;

public MandjeFacade()
{
bestellijn = new BestellijnDTO();
}

public void legProductInMandje(int bierNr, int aantal)
{
bestellijn.setBierNr(bierNr);
bestellijn.setAantal(aantal);
mandje.add(bestellijn);
}

...





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???

mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
Campbell Ritchie wrote:And welcome to JavaRanch


thank you :-)
mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Mike, please create a new thread in the appropriate forum instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Rob Prime wrote:Mike, please create a new thread in the appropriate forum instead.
I shall simply move this thread.
mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
Still looking for the answer :p

 
jQuery in Action, 2nd edition
 
subject: Collection problem
 
Similar Threads
Another nested loop question...
Final variables
Java Collection doubt #1
Is there a way to get an absolute row id for a JTable model
Cast problem