This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes adding items to arraylist through constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 1778
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: 1778
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: 24081
    
  15

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: adding items to arraylist through constructor
 
Similar Threads
how to creat a searching code?
Arrays are confusing
Why is my type unexpected? required variable found value
New Objects keeping old values
collection exercize