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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:14:32+00:00 2026-05-20T10:14:32+00:00

this.size = 9; this.populationSize = 10; Random rand = new Random(); Integer[][] easy1 =

  • 0
        this.size = 9;
        this.populationSize = 10;
        Random rand = new Random();


        Integer[][] easy1 = new Integer[size][size];
        easy1 = this.initializeEasy1(easy1);
        this.sudokuArray = new Sudoku[this.populationSize];
        for (int i = 0; i < this.sudokuArray.length; i++){
            long seed = rand.nextLong();
            System.out.println("" + seed);
            this.sudokuArray[i] = new Sudoku(easy1, this.size, seed);
        }

I am building an evolutionary sudoku solver and I am having a problem where the last Sudoku object is overwriting all the other objects in the array. Where in the code did I mess up?

/edit here is the constructor of the class

public Sudoku(Integer[][] givensGrid, int s, long seed){
    this.size = s;
    this.givens = givensGrid;
    this.grid = this.givens.clone();
    Random rand = new Random(seed);

    System.out.println("Random " + rand.nextInt());
    // step though each row of the grid
    for (int i = 0; i < size; i++){
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        numbers = this.makeNumbers(numbers);

        // step through each column to find the givens and remove from numbers
        for (int j = 0; j < size; j++){
            if (this.grid[i][j] != 0){
                numbers.remove(this.grid[i][j]);
            }
        }
        // go back through the row and assign the numbers randomly
        for (int j = 0; j < size; j++){
            if (this.grid[i][j] == 0){
                int r = rand.nextInt(numbers.size());
                this.grid[i][j] = numbers.get(r);
                numbers.remove(r);
            }
        }
    }
    System.out.println("=============");
    System.out.println(this.toString());
}

here is the fixed code

this.size = 9;
    this.populationSize = 10;
    Random rand = new Random();


    Integer[][] easy1 = new Integer[size][size];
    this.sudokuArray = new Sudoku[this.populationSize];
    for (int i = 0; i < this.sudokuArray.length; i++){
        long seed = rand.nextLong();
        easy1 = new Integer[size][size];
        easy1 = this.initializeEasy1(easy1);
        System.out.println("" + seed);
        this.sudokuArray[i] = new Sudoku(easy1, this.size, seed);
    }
  • 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-05-20T10:14:32+00:00Added an answer on May 20, 2026 at 10:14 am

    When you declare easy1 to be a new Integer 2D array, you are saying that easy1 is a reference to 1 2D array object.

    You are then adding a new Sudoku object which references the same 2D array, since you are passing the reference to it. Thus, all of your sudoku objects are referencing just 1 2D array, which is probably not what you want.

    I would rather change this line to:

    this.sudokuArray[i] = new Sudoku(new Integer[size][size], size, seed);
    

    Does that make sense?

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

Sidebar

Related Questions

public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K
I have this struct: struct Map { public int Size; public Map ( int
Is this list-initialization of an array of unknown size valid in C++0x? int main()
I have this ParkingLot.java public class ParkingLot { private final int size; private Car[]
int width, height; width = this.Size.Width; height = this.Size.Height; width /= 3; height /=
I'm trying to write a size function like this: size(void *p,int size); Which would
I noticed that some stylesheets have something like this: body { font-size: 62.5%/1.2em; }
This should reduce the executable size quite a bit in some of my very
This question was inspired by a similar question: How does delete[] know the size
In this code, for vector size, n >=32767, it gives segmentation fault, but upto

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.