If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Creating an ArrayList as an attribute

Hello All,

Maybe it's time I started utilizing forums to help with my programs. I was assigned to create a program which connects to a database, access two tables, take information from the tables and store them as attributes of 2 objects. The only part that is hard for me is adding an ArrayList as an attribute. I am accessing a loan table and a payment table. Everything from the loan table is taken and assigned to the loan object and the payment table to payment object. The thing is every "loan" has payments. He wants us to add an ArrayList called "ArrayList<Payment>" to the loan class as an attribute of the loan object. I am having trouble creating an instance of this in my loan class... can anyone help? This assignment was already graded I just want to know how to do this for future reference (probably on my final exam as well). Thank you.

Re: Creating an ArrayList as an attribute

In your constructor you are creating a new instance of ArrayList that survives only as long as the method is alive. Instead, you have the ArrayList in the parameters, and the instance variable... simply do this:

this.payments=p;

Or you could check if parameter p is null, in which case declare payments as a new ArrayList

this.payments=new ArrayList<Payment>()

That way you avoid having a possible null list.

For some odd reason, you are tumbling on and on from one declaration of an ArrayList to another. Regardless that the variable names equal, they are not the same object, when declared anew (as in ArrayList<Payment> payments=new ArrayList<Payment>(), does not reference your instance variable where as payments=new ArrayList<Payment>() does). There is no namespace collision, because they are not declared within the same brackets. Also, there is a declaration with no variable name what so ever. You can experiment this, by eg. changing the name of the parameter ArrayList also to payments.

Much adviced that you consult the class material, and/or Sun Java tutorials on how variable life span goes.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.