aspose file tools*
The moose likes Beginning Java and the fly likes adding items to arraylist through constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding items to arraylist through constructor" Watch "adding items to arraylist through constructor" New topic
Author

adding items to arraylist through constructor

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
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
Bartender

Joined: May 31, 2011
Posts: 1776
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Mar 27, 2011
Posts: 44
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding items to arraylist through constructor