Results 1 to 5 of 5
  1. #1
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default 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.

    Java Code:
    accounts[0].setBalance(50);
    accounts[1].setBalance(50);
    or will

    accounts[].setBalance(50);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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};

  4. #4
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: How can I set default value to Array

    Thanks Fubarable I will try that.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

Similar Threads

  1. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 05:08 PM
  2. How array elements gets default value
    By Veangat in forum New To Java
    Replies: 1
    Last Post: 03-07-2010, 02:29 PM
  3. default button SWT
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-07-2009, 05:12 PM
  4. Replies: 1
    Last Post: 03-10-2009, 02:12 PM
  5. Default Button
    By hiranya in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2007, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •