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 9209445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:50:40+00:00 2026-06-18T00:50:40+00:00

I am writing a program for my IB Math internal assessment. I have the

  • 0

I am writing a program for my IB Math internal assessment.

I have the classes Calc, Gamble, Inventory, Player, and Test.
I have a field instance variable Players (instantiated in the constructor) and the method playrounds and that is where the problem is.
The source for the method is below:

    ArrayList<Player> Players;

    public ArrayList<ArrayList<ArrayList<Player>>> playrounds(boolean randombets, double initialmoney, double initialbet,
        double betrange, int numberofrounds){

    ArrayList<Player> winningPlayers = new ArrayList<>(0);
    ArrayList<ArrayList<Player>> m = new ArrayList<>(0);
    for(int b = 0; b < numberofrounds; b++){
        if(firstround){
            firstround = false;
            preparePlayersfirst(randombets, initialmoney, initialbet, betrange);
            winningPlayers.clear();
            winningPlayers.trimToSize();
            m.clear();
            m.trimToSize();
        }
        else{
            preparePlayersnext(randombets, initialbet, betrange);
        }
        for(int a = 0; a < playingPlayers.size(); a++){
            Player u = playingPlayers.get(a);
            u.rolldice();
            u.checkdice();
            Players.set(a, u);
            playingPlayers.set(a, u);
        }
        Player t = getwinningPlayer();
        for(int a = 0; a < playingPlayers.size(); a++){
            Player u = playingPlayers.get(a);
            u.setprofits(t, this);
            Players.set(a, u);
            playingPlayers.set(a, u);
        }
        distributegainslosses();
        removebankruptPlayers();
        winningPlayers.add(t);
        m.add(Players);
        for(int x = 0; x < m.size(); x++){
            for(Player y: m.get(x)){
                for(int z = 0; z < y.getdice().size(); z++){
                    System.out.print(y.getdie(z) + " ");
                }
                System.out.print("\t");
            }
            System.out.println();
        }
        System.out.println();
    }
    ArrayList<ArrayList<ArrayList<Player>>> results = new ArrayList<>(0);
    ArrayList<ArrayList<Player>> t = new ArrayList<>(0);
    t.add(winningPlayers);
    results.add(t);
    results.add(m);

    return results;

}

The for loop to print the values inside the variable m gives an output like this:
2 3

6 4
6 4

5 5
5 5
5 5

6 1
6 1
6 1
6 1

The expected output is something like this:
2 3

2 3
6 4

2 3
6 4
5 5

2 3
6 4
5 5
6 1

I know that the way I print the values is not wrong. It doesn’t print duplicates.
The if(firstround) and the else does not have to do with the problem.
Any help would be greatly appreciated.

-Edit-
@NPE: I tried one of the solutions from the place you suggested to look at. It still doesn’t work. Here’s the code for what I did:

    public ArrayList<Player> clonePlayers(){

        ArrayList<Player> clone = new ArrayList<>(0);
        for(Player a: Players){
            clone.add(new Player(a));
        }

        return clone;

    }

Did I do something wrong?

  • 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-18T00:50:41+00:00Added an answer on June 18, 2026 at 12:50 am

    The problem is that you insert the same reference into m over and over again:

       m.add(Players);
    

    If you need the entries of m to be independent of one another they have to be different objects, not references to the same object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I'm new to flash in general and have been writing a program with two
I am writing a program to try to solve a math problem. I need
I am writing a drawing program, Whyteboard -- http://code.google.com/p/whyteboard/ I have implemented image rotating
I'm writing a program in C that needs to do some fast math calculations.
writing a basic math program to help me understand python math calculation. if I
I have been going crazy on this program im writing. My decks dont seem
I am writing a matrix class for a math project, and I have a
I have a program I'm writing in which the user has the option to
At work training, I'm writing a Java (in which I have 0 experience) program

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.