Java 将二维阵列拆分为多个较小阵列的阵列?
我有一个9x9 2D阵列,我想将其拆分为一个9 3x3 2D阵列 以下是我到目前为止的情况:Java 将二维阵列拆分为多个较小阵列的阵列?,java,arrays,matrix,split,Java,Arrays,Matrix,Split,我有一个9x9 2D阵列,我想将其拆分为一个9 3x3 2D阵列 以下是我到目前为止的情况: 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 } }; 之后: nw[][]将由板[0][
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 } };
之后:
将由nw[][]
到板[0][0]
组成板[3][3]
由n[][]
到board[4][0]
board[6][3]
- 等等
在不手动将每个元素添加到正确部分的情况下,执行此操作的最佳方法是什么?
java.util.Arrays.copyOfRange()
可以帮助您完成部分工作。java不允许对数组进行子索引
在C语言中,您所指的是很可能的,但在Java中,您需要:
- 将数据复制到新阵列
- 使用从存储中提取的自定义类
foo[0]
无法永久引用另一个数组的元素bar[3]
如果要使用int[][]
,必须复制数组Arrays.copyOfRange
和System.arraycopy
将是最有效的选择,但在数独游戏中,这显然没有多大区别
对于第二种方法,编写一个定制的矩阵类。比如说
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!
添加大小信息和类似的东西。
你会考虑下面的解决方案“手册”吗?申报
等等。听起来像Sudoko强>
上次我解决了这个问题,你可以将9X9数组分成(9)个3X3数组
您可以这样做:
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];
}
}
}
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);
}
}
getSection(s,n,0,1);
getSection(s,ne,0,2);
getSection(s,w,1,0);
第1节:
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];
}
}
}
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);
}
}
getSection(s,n,0,1);
getSection(s,ne,0,2);
getSection(s,w,1,0);
第2节:
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];
}
}
}
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);
}
}
getSection(s,n,0,1);
getSection(s,ne,0,2);
getSection(s,w,1,0);
第3节:
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];
}
}
}
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);
}
}
getSection(s,n,0,1);
getSection(s,ne,0,2);
getSection(s,w,1,0);
等
<强>注意C++中的问题的解决方案
但是,在java和C++中,主要思想是相同的。< /强> < /p>是否真的需要每个分区的单独数组,或者只想用一种方法指向原始数组中的那些区域?如果是后者,您可能会创建一个类,其中每个实例表示每个特定的部分,并且您可以从board[]]检索实际值,但该类的抽象将帮助您轻松地将其视为一个部分。只是一个想法…希望不要太混乱。闻起来像数独游戏…我建议你使用for循环,因为你知道board
中“subboard”的第一行和最后一行以及第一列和最后一列。你需要一个=new int[3][3]
。目前,您正在初始化se
。您的代码没有编译:您需要向部分
或系统添加另一个维度。arraycopy作为一个相当快速的实现。如何将copyOfRange()用于二维数组?我能找到的每一个关于如何使用它的例子都只显示1D数组。哈哈,我记得我刚开始的时候,我试着像在python中那样子列出一个数组。几年后,我和几个朋友正在把它添加到一种类似Java的语言中,哈哈。我喜欢这种方法的面向对象方法。非常干净的代码