• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creation of ArrayList

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was creating a arraylist.here is the code



the code is working fine...however what I require is if I run the code second time it doesn't create a new list but return the same list which was created earlier...is it possible
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi deepak,

Answer to your question: Yes, it is possible.

In your current code, you are almost doing it (checking if list is null, and then only assign it).

But the problem is, 'list' is an instance variable, and every time you create a new object of ListTest, the list variable in new object will be null, and hence you'll end up creating new lists.

You can avoid it by:
1) Either keep list as static - so that same list will be shared by all objects of ListTest, and your current makeList will work fine (except you'll need to make it thread-safe if you are planning to run this code in multi-threaded environment).
2) Or, make ListTest class itself as singleton. That way, only one object of ListTest will exist (and obviously, only one list will be there).

I hope this answers your doubt.
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

thanks for the reply...what my requirement state that for every user that log in in there should be a list for that user.It will act like a cache.

I have to use core java as i am not using servlet
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if every user needs separate ArrayList, then singleton is not an option.

In fact, I would go ahead and create a new ArrayList for each user. Only thing is - there should be only one ListTest object per user.
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi deepak.

As far as I have understood ( correct me if I am wrong ) , you want to have a single List object for each user and that list will be initialized when ever , e.g a new user entity is created . And after that , any call to the already created user will return the existing List of that user only .

So my best guess would be to

Create a User class , with a List object injected into the class. And keep the list object as singleton.


This way no matter how many times the same user comes, the same list will be returned. But for every new User , a new List will be created.

My main looks like this


I hope I was able to help you out.
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is some poor design in that class. You have a thread‑unsafe method, and you are calling a public non‑final method from a constructor. If you want a shared List where each instance of User has its own instance (why? what a strange requirement), try this:-You have also got poor style in the names of your fields.
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Campbell ,

Thank you for the corrections and the greetings :-) I was just giving a rough idea about how the class may look , but I will surely take care of proper conventions and coding style next time . This was just my first post :-)
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why there is this kind of requirement because-the list will act as cache where it will hold some kind kind of numbers say for example account number which a particular user can access.The access depends on this list.If the number is on the list for that user that means the user can access the record for that number.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak carter wrote:Why there is this kind of requirement because-the list will act as cache where it will hold some kind kind of numbers say for example account number which a particular user can access.The access depends on this list.If the number is on the list for that user that means the user can access the record for that number.


OK, well:
1. You didn't explain any of this at the start. You simply asked why your code didn't do what you wanted.
2. If you want to keep data, be it a List, Set, array or BLOB, of things for a "user", then you will probably want some way of finding them by that user (or not). And the best way to do that in Java is with a Map (java.util.Map) - of which, there are several different types.

Winston
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic