• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding items to arraylist through constructor

 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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"?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic