File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection problem

 
mike ickx
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
mike ickx
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to JavaRanch


thank you :-)
 
mike ickx
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, please create a new thread in the appropriate forum instead.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Mike, please create a new thread in the appropriate forum instead.
I shall simply move this thread.
 
mike ickx
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still looking for the answer :p

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic