• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
mike ickx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And welcome to JavaRanch



thank you :-)
 
mike ickx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike, please create a new thread in the appropriate forum instead.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still looking for the answer :p

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic