Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:20:04+00:00 2026-06-11T22:20:04+00:00

I read docs about java recorsion and I thought I have understood it, but

  • 0

I read docs about java recorsion and I thought I have understood it, but when I try to use it in the following example, it does not work as expected.

I a class account, which has amount and can have forther subAccount. I would have implemented one method getSum, which has to return the summ of the amount of the account and amount of all of its subaccount. In the following code, the call of the method getSumm() should return 550, but it behaves strange. can somebody help please?

public class Balance{
    ArrayList<Balance> subAccounts = new ArrayList<Balance>();
    String accountID = null;
    Double amount = null;
    double result=0;
    public double getSum(ArrayList<Balance> subAccounts){

        if(subAccounts !=null && subAccounts.size()>0){
            for (int i = 0; i < subAccounts.size(); i++) {

            result = result + getSum(subAccounts.get(i).subAccounts);

            }
        }
            else {
                return amount;
            }

        return result;
    }


    public static void main(String[] args) {
        Balance bs1 = new Balance();
        Balance bs2 = new Balance();
        Balance bs3 = new Balance();

        bs1.amount=100.0;
        bs2.amount=150.0;
        bs3.amount=300.0;
        ArrayList<Balance> subAccounts1 = new ArrayList<Balance>();

        bs2.subAccounts=null;
        bs3.subAccounts=null;
        subAccounts1.add(bs2);
        subAccounts1.add(bs3);
        bs1.subAccounts=subAccounts1;
        double sum= bs1.getSum(subAccounts1);
        System.out.println(sum);

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:20:06+00:00Added an answer on June 11, 2026 at 10:20 pm

    Your logic was a little iffy.

    The getSum shouldn’t take an argument because the subAccounts member variable is part of the object already.

    Also you were collecting the internal sum in a member variable. Use local variables to avoid problems.

    As everyone has everyone has already said… make sure you are not actually using recursion for this. This is simply a demonstration of how to use recusion so that is ok.

    Here is the corrected program:

    import java.util.ArrayList;
    
    public class Balance {
        ArrayList<Balance> subAccounts = new ArrayList<Balance>();
        String accountID = null;
        Double amount = null;
    
        public double getSum() {
            if (subAccounts != null) {
                Double sum = 0.0;
                for (int i = 0; i < subAccounts.size(); i++) {
                    sum += subAccounts.get(i).getSum();
                }
                return amount + sum;
            } else {
                return amount;
            }       
        }
    
        public static void main(String[] args) {
            Balance bs1 = new Balance();
            Balance bs2 = new Balance();
            Balance bs3 = new Balance();
            bs1.amount = 100.0;
            bs2.amount = 150.0;
            bs3.amount = 300.0;
            ArrayList<Balance> subAccounts1 = new ArrayList<Balance>();
            bs2.subAccounts = null;
            bs3.subAccounts = null;
            subAccounts1.add(bs2);
            subAccounts1.add(bs3);
            bs1.subAccounts = subAccounts1;
            double sum = bs1.getSum();
            System.out.println(sum);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read some docs about the .NET Garbage Collector but i still have some
I've read here: https://developers.google.com/web-search/docs/?hl=iw-IL#CustomBranding about how to use Google Search in Java. Still, I
I have read about Java enums and use them regularly. However, I don't understand
I have read the docs and everything but I'm very confused. I never needed
I have already read the hibernate docs about concurrency and I think, I have
I read some docs about md5, it said that its 128 bits, but why
I've read the docs about Android Resources and I think I've understood the best-matching
I have doubts about Async WebService (Java) that supports the Callback mechanism. I read
as someone's who's just starting to get into Android/Java programming, I've read the docs
I have read all the docs I can get my hands on and google'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.