Java实现二维数组拆分为四个子数组的方法咨询
二维数组拆分为四个子数组的Java实现建议
需求说明
输入4x4二维数组:
1 5 6 2 3 1 6 5 6 3 9 4 3 8 4 2
期望拆分输出四个2x2子数组:
1 5 3 1 6 2 6 5 6 3 3 8 9 4 4 2
实现建议
你计划通过创建四个新二维数组填充后打印的思路是可行的,以下是具体优化与实现步骤:
1. 先处理输入校验与尺寸计算
首先要确保输入是行列数相等的偶数方阵,避免非法输入导致异常;然后计算子数组的尺寸halfSize = grid.length / 2。
2. 初始化子数组并修正拼写错误
你代码里的BottonRight是拼写错误,应改为BottomRight,直接按子数组尺寸初始化四个数组:
int halfSize = grid.length / 2; double[][] topLeft = new double[halfSize][halfSize]; double[][] topRight = new double[halfSize][halfSize]; double[][] bottomLeft = new double[halfSize][halfSize]; double[][] bottomRight = new double[halfSize][halfSize];
3. 高效填充子数组
可以用System.arraycopy替代嵌套循环填充,代码更简洁且效率更高:
// 填充左上区域 for (int i = 0; i < halfSize; i++) { System.arraycopy(grid[i], 0, topLeft[i], 0, halfSize); } // 填充右上区域 for (int i = 0; i < halfSize; i++) { System.arraycopy(grid[i], halfSize, topRight[i], 0, halfSize); } // 填充左下区域 for (int i = halfSize; i < grid.length; i++) { System.arraycopy(grid[i], 0, bottomLeft[i - halfSize], 0, halfSize); } // 填充右下区域 for (int i = halfSize; i < grid.length; i++) { System.arraycopy(grid[i], halfSize, bottomRight[i - halfSize], 0, halfSize); }
4. 复用打印逻辑避免重复代码
单独写一个打印子数组的方法,减少冗余代码:
private static void printSubArray(double[][] subArr) { for (double[] row : subArr) { for (double num : row) { System.out.print(num + " "); } System.out.println(); } }
5. 调用打印方法输出结果
在splitGrid方法中调用打印逻辑,每个子数组后添加空行分隔:
printSubArray(topLeft); System.out.println(); printSubArray(topRight); System.out.println(); printSubArray(bottomLeft); System.out.println(); printSubArray(bottomRight);
完整修改后的代码
public class problem { public static void main(String[] args) { double[][] array1 = { {1, 5, 6, 2}, {3, 1, 6, 5}, {6, 3, 9, 4}, {3, 8, 4, 2} }; splitGrid(array1); } public static void splitGrid(double[][] grid) { // 校验输入是否为偶数方阵 if (grid.length == 0 || grid.length % 2 != 0 || grid[0].length != grid.length) { System.out.println("输入必须为行列数相等的偶数方阵"); return; } int halfSize = grid.length / 2; // 初始化四个子数组 double[][] topLeft = new double[halfSize][halfSize]; double[][] topRight = new double[halfSize][halfSize]; double[][] bottomLeft = new double[halfSize][halfSize]; double[][] bottomRight = new double[halfSize][halfSize]; // 填充左上区域 for (int i = 0; i < halfSize; i++) { System.arraycopy(grid[i], 0, topLeft[i], 0, halfSize); } // 填充右上区域 for (int i = 0; i < halfSize; i++) { System.arraycopy(grid[i], halfSize, topRight[i], 0, halfSize); } // 填充左下区域 for (int i = halfSize; i < grid.length; i++) { System.arraycopy(grid[i], 0, bottomLeft[i - halfSize], 0, halfSize); } // 填充右下区域 for (int i = halfSize; i < grid.length; i++) { System.arraycopy(grid[i], halfSize, bottomRight[i - halfSize], 0, halfSize); } // 打印所有子数组 printSubArray(topLeft); System.out.println(); printSubArray(topRight); System.out.println(); printSubArray(bottomLeft); System.out.println(); printSubArray(bottomRight); } private static void printSubArray(double[][] subArr) { for (double[] row : subArr) { for (double num : row) { System.out.print(num + " "); } System.out.println(); } } }
内容的提问来源于stack exchange,提问作者MazaPan616
相关产品推荐
相关产品推荐

