1

I have an ArrayList called participatingUsers. Person has Person.money and Person.name that are interesting.

What I want to do is check the ArrayList against itself...

So I have this code

    for (Person debtHaver : this.participatingUsers) {

        // If they're in debt...
        if (debtHaver.getMoney() < 0) {

            // With someone...
            for (Person personToPay : this.participatingUsers) {

                // That's not themselves...
                if (!debtHaver.getName().equals(personToPay.getName())) {
                    // See if the personToPay is ranked higher than the
                    // debtHaver...
                    if (personToPay.getMoney() > 0) {

                        // If the debtee can pay the debter in full
                        if (-debtHaver.getMoney() <= personToPay.getMoney()) {
                            payment += debtHaver.getName() + " has to pay " + personToPay.getName() + " " + -debtHaver.getMoney() + "\n";
                            debtHaver.increaseMoney(-debtHaver.getMoney());
                            personToPay.decreaseMoney(-debtHaver.getMoney());
                        }

                        if (-debtHaver.getMoney() > personToPay.getMoney())
                        {
                            //But if he can't pay in full... Just pay the small bit you can pay.
                            payment += debtHaver.getName() + " has to pay " + personToPay.getName() + " " + personToPay.getMoney() + "\n";
                            debtHaver.increaseMoney(personToPay.getMoney());
                            personToPay.decreaseMoney(personToPay.getMoney());
                        }
                    }
                }
            }
        }
    }
    return payment;

Basically, I have a double for loop where I check each person against itself. If someone is in debt and has a negative amount of money, seek if there is someone who they can pay, then pay that person. The thing is, personToPay is not being updated in the arrayList debtHaver is in. I'm basically editing two different ArrayLists instead of the same one. What's the best way to deal with this issue?

2
  • 2
    'personToPay is not being updated in the arrayList debtHaver is in' Why not? It should be, the lists are certainly not copied and the objects are stored by reference (not by copy). Do you get an error or undesired behavior? Commented Dec 16, 2012 at 11:57
  • Furthermore, I think you have an error in your checks whether one has to pay. As the two if-statements are their opposite, so there is always someone paying even though both persons are in debt or both are not... Commented Dec 16, 2012 at 12:01

1 Answer 1

2

You are editing the same list. The problem is probably in this code:

debtHaver.increaseMoney(-debtHaver.getMoney());
personToPay.decreaseMoney(-debtHaver.getMoney());

You are putting debtHaver's amount to zero by the first line. And then you try to modify personToPay with zero amount. Just swap two lines of code and it should work:

personToPay.decreaseMoney(-debtHaver.getMoney());
debtHaver.increaseMoney(-debtHaver.getMoney());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.