• 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

adding items to arraylist through constructor

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding =)



for this piece of code



I'll also like to add a list of recommendations into Wallet's arraylist when item is added to inventory. I've been fetting for hours...pouring into tutorial and code samples. how can this be done?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the real code. My choice will be to construct the recommendations in a List<String> in the Test class and just assign it to the Item class recommendations List. Please use List<String> rather than ArrayList<String>.

If you still want to assign recommendations in the Item class constructor, then you can pass a String array objects and add each recommendation String into the ArrayList like below.



 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please PostRealCode... for sample the Wallet class is shown as extending the Product class but according to the context it should be Item.. Or else the Product class definition is not posted.
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:And please PostRealCode... for sample the Wallet class is shown as extending the Product class but according to the context it should be Item.. Or else the Product class definition is not posted.



i've fixed those bits, do you mean there is no way I can add Strings to Recommendation arraylist as I instantiate "wallet"?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Smith wrote:
i've fixed those bits, do you mean there is no way I can add Strings to Recommendation arraylist as I instantiate "wallet"?



Not as it stands, because Wallet doesn't provide any way to do it. Add a List<String> or String[]constructor parameter, though, and then it'd be easy.
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:

Randy Smith wrote:
i've fixed those bits, do you mean there is no way I can add Strings to Recommendation arraylist as I instantiate "wallet"?



Not as it stands, because Wallet doesn't provide any way to do it. Add a List<String> or String[]constructor parameter, though, and then it'd be easy.



how about this?



I've tested the above code and it does work. I would love more input if there's a better way.
 
reply
    Bookmark Topic Watch Topic
  • New Topic