我正在創建一種將二維陣列拆分為 4 部分的方法。
例如,如果網格是:
1 5 6 2
3 1 6 5
6 3 9 4
3 8 4 2
該方法應回傳:
1 5
3 1
6 2
6 5
6 3
3 8
9 4
4 2
我的代碼僅將二維陣列列印到網格中。我的想法是在方法中創建 4 個新網格,然后使用一系列 forloop 和 if 陳述句列印每個新網格。這就是該方法中有 4 個二維陣列的原因。有什么建議么?
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 ) {
//Create four new grids here
double[][] TopLeft;
double[][] TopRight;
double[][] BottomLeft;
double[][] BottonRight;
// Filling the top-left grid
for( int i = 0; i < grid.length; i ) {
System.out.println();
for(int j = 0; j < grid[i].length; j ) {
System.out.print(grid[i][j] " " );
}
}
}
}
uj5u.com熱心網友回復:
有很多方法可以做到這一點,其中一種(可能是最安全的!)當然是copy來自標準庫Arrays類的方法。但是,如果您希望通過這個來發展您的“手動”演算法技能,那么以下內容可能是合適的:
public static void main(String[] args) {
double[][] fourByFourArray = {{1, 5, 6, 2},
{3, 1, 6, 5},
{6, 3, 9, 4},
{3, 8, 4, 2}};
System.out.println("Printing 4x4 array: ");
splitGridInto4equalParts(fourByFourArray);
System.out.println("Printing 6x6 array: ");
double[][] sixBySixArray = {{1, 5, 7, 6, 8, 2},
{3, 1, 9, 6, 1, 5},
{6, 3, 20, 10, 2, 4},
{3, 8, 31, 2, 5, 2},
{3, 1, 9, 6, 1, 5},
{3, 8, 31, 2, 5, 2},
};
splitGridInto4equalParts(sixBySixArray);
}
public static void splitGridInto4equalParts(double[][] grid) {
//assuming grid has equal sides
var gridSide = grid.length;
//assuming grid side is even number
var half = gridSide / 2;
//filling arrays
double[][] topLeft = getPart(grid, half, 0, 0, half, half);
double[][] topRight = getPart(grid, half, 0, half, half, gridSide);
double[][] bottomLeft = getPart(grid, half, half, 0, gridSide, half);
double[][] bottomRight = getPart(grid, half, half, half, gridSide, gridSide);
//printing
printGridPart(half, topLeft);
printGridPart(half, topRight);
printGridPart(half, bottomLeft);
printGridPart(half, bottomRight);
}
private static double[][] getPart(double[][] grid, int partSideLength, int gridRow, int gridCol, int gridRowLimit, int gridColLimit) {
double[][] gridPart = new double[partSideLength][partSideLength];
int resRow = 0, resCol = 0;
//saving original grid column parameter, in order to reset it when execution moves on new grid row
int origGridCol = gridCol;
while (gridRow < gridRowLimit) {
while (gridCol < gridColLimit) {
gridPart[resRow][resCol] = grid[gridRow][gridCol];
//checking whether result column has reached limit in order to move to next row and reset column
if (resCol == partSideLength - 1) {
resRow ;
resCol = 0;
break;
}
//moving one column to the right for both result and grid
resCol ;
gridCol ;
}
//checking whether result row has reached limit, if so exit from traversing rows
if (resRow == partSideLength) {
break;
}
gridCol = origGridCol;
gridRow ;
}
return gridPart;
}
private static void printGridPart(int gridLength, double[][] gridPart) {
for (int i = 0; i < gridLength; i ) {
for (int j = 0; j < gridLength; j ) {
System.out.print(gridPart[i][j] " ");
}
System.out.println();
}
System.out.println();
}
這輸出:
Printing 4x4 array:
1.0 5.0
3.0 1.0
6.0 2.0
6.0 5.0
6.0 3.0
3.0 8.0
9.0 4.0
4.0 2.0
Printing 6x6 array:
1.0 5.0 7.0
3.0 1.0 9.0
6.0 3.0 20.0
6.0 8.0 2.0
6.0 1.0 5.0
10.0 2.0 4.0
3.0 8.0 31.0
3.0 1.0 9.0
3.0 8.0 31.0
2.0 5.0 2.0
6.0 1.0 5.0
2.0 5.0 2.0
uj5u.com熱心網友回復:
您可以回傳一個包含{topLeft, topRight, bottomLeft, bottomRight}這樣的網格陣列。
public static void main(String[] args) {
double[][] array1 = {
{1, 5, 6, 2},
{3, 1, 6, 5},
{6, 3, 9, 4},
{3, 8, 4, 2}};
double[][][] result = splitGrid(array1);
for (double[][] grid : result) {
for (double[] row : grid)
System.out.println(Arrays.toString(row));
System.out.println();
}
}
public static double[][][] splitGrid(double[][] grid) {
int rows = grid.length, cols = grid[0].length;
int subRows = rows / 2, subCols = cols / 2;
double[][][] result = {
new double[subRows][], new double[subRows][],
new double[rows - subRows][], new double[rows - subRows][]};
for (int i = 0; i < rows; i ) {
if (i < subRows) {
result[0][i] = Arrays.copyOf(grid[i], subCols);
result[1][i] = Arrays.copyOfRange(grid[i], subCols, cols);
} else {
result[2][i - subRows] = Arrays.copyOf(grid[i], subCols);
result[3][i - subRows] = Arrays.copyOfRange(grid[i], subCols, cols);
}
}
return result;
}
輸出:
[1.0, 5.0]
[3.0, 1.0]
[6.0, 2.0]
[6.0, 5.0]
[6.0, 3.0]
[3.0, 8.0]
[9.0, 4.0]
[4.0, 2.0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524664.html
標籤:爪哇数组for循环if 语句数学
上一篇:接收整數n>0并判斷其數字(以10為底)是否形成非遞減序列的函式(每個數字>=到前一個數字)
下一篇:如何找到負數的乘法因子
