1

I have a 9x9 2D array that I want to split into an array of 9 3x3 2D arrays.

Here is what I have so far:

int board[][] = new int[9][9];

// Fill board with numbers...

int[][] nw, n, ne, w, c, e, sw, s, se = new int[3][3];
int[][] sections = { { nw, n, ne }, { w, c, e }, { sw, s, se } };

Afterwards:

  • nw[][] would consist of board[0][0] thru board[3][3].
  • n[][] consists of board[4][0] thru board[6][3]
  • etc.

What's the best way to do this without manually adding every single element to the correct section?

4
  • 3
    Do you really need separate arrays for each section, or do you just want a way to point to those areas within the original array? If the latter, you could probably create a class where each instance represents each particular section and as you call it you retrieve the actual value from board[][] but the abstraction of the class would help you easily see it as just a section. Just an idea...Hope that's not too confusing. Commented May 30, 2013 at 17:58
  • Smells like sudoku...I suggest that you use a for loop since you know the first and last rows and the first and last columns for the "subboards" in board. Commented May 30, 2013 at 18:01
  • You'll need a = new int[3][3] for each of those. Currently you're just initializing se. Commented May 30, 2013 at 18:04
  • Your code doesn't compile: you need to add another dimension to sections Commented May 30, 2013 at 20:58

4 Answers 4

2

java.util.Arrays.copyOfRange() can get you part of the way.

Sign up to request clarification or add additional context in comments.

2 Comments

Or System.arraycopy as rather fast implementation.
How do I use copyOfRange() with 2D arrays? Every example I can find of how to use it shows only 1D arrays.
1

sounds like Sudoko!

I solved like this problem last time you can divide 9X9 array into (9) 3X3 arrays

you can do that in this way :

void getSection(int board[9][9],int result[3][3],int x, int y) 
{
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            result[i][j] = board[3*x+i][3*y+j];
        }
    }
}

after that call getSection for every section in 9X9 array :

 int s[9][9];
 int s1[3][3];

     for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
              getSection(s,s1,i,j);
            }
        }

or you can do it manually :

section 0 :

  getSection(s,nw,0,0);

section 1 :

 getSection(s,n,0,1);

section 2 :

 getSection(s,ne,0,2);

section 3 :

 getSection(s,w,1,0);

etc.

note that the solution of your question in c++ but the main idea is same in java and c++.

Comments

0

Java does not allow subindexing of arrays.

What you are referring to is trivially possible in C, but in Java you will need to either:

  • copy the data to new arrays
  • use a custom class abstracting from storage.

In java, there is no way that foo[0] permanently refers to the element bar[3] of another array.

If you want to work with int[][], you'll have to copy the arrays. Arrays.copyOfRange and System.arraycopy will be the most efficient choices, but at Sudoku size it does obviously not make much of a difference.

For the second approach, write a custom Matrix class. For example

class Matrix {
  int[] flatStorage;
  int[] offsets;

  Matrix(int[] flatStorage, int[] offsets) {
    this.flatStorage = flatStorage;
    this.offsets = offsets;
  }

  void set(int x, int y, int val) {
    flatStorage[ offsets[x] + y ] = val;
  }

  int get(int x, int y) {
    return flatStorage[ offsets[x] + y ];
  }
}

int[] sharedStorage = new int[27];
Arrays.fill(sharedStorage, -1); // Initialize with -1

int[] allOffsets = new int[]{0,9,18, 27,36,45, 54,63,72};
Matrix nineByNine = new Matrix(sharedStorage, allOffsets);
Matrix northEast = new Matrix(sharedStorage, new int[]{6,15,24});
Matrix southEast = new Matrix(sharedStorage, new int[]{60,69,78});

nineByNine.set(1,7, 2); // Write to middle of upper right quadrant
System.err.println(northEast.get(1, 1)); // Read - should be 2!

Add size information and similar things yourself.

1 Comment

Haha, I remember when I was starting I tried to sublist an array like it would have been done in python. Years later, a few buddies and I are adding this in to a language similar to Java haha. I like the OOP approach to this method. Very clean code
0

Would you consider the following solution "manual"? Declare

int[][] getSection(int x, int y) {
    int[][] result = new int[3][3];
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            result[i][j] = board[3+x+1][3*y+j];
        }
    }
    return result;
}

then call

nw = getSection(0,0);
n = getSection(1,0);

etc.

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.