How can I set default value to Array
Hi
How do I assign a new default value to array variable. In my account class I have set the variable balance to be default 0. I am now creating an array of 10 accounts and want to give them a default balance of 50. Do I still have to set each of them like so or is there a way to set thedefault balance for the whole array.
Code:
accounts[0].setBalance(50);
accounts[1].setBalance(50);
or will
accounts[].setBalance(50);
Re: How can I set default value to Array
You will need to use a for loop or foreach loop to loop through the array, setting the balance of each item held by the array.
Re: How can I set default value to Array
I think it is best to do that with a constructor. Something like:
public Account (int balance = 0) {this.balance = balance};
Re: How can I set default value to Array
Thanks Fubarable I will try that.
Re: How can I set default value to Array
Sorry about my earlier reply, I don't think it works in Java. I thought it is like C++ but apparently it doesn't work in Java.