Home > database >  method that splits a 2D array into 4 parts in Java
method that splits a 2D array into 4 parts in Java

Time:10-31

I'm creating a method that splits a 2D array into 4 parts.

For example if the grid is:

1 5 6 2
3 1 6 5
6 3 9 4
3 8 4 2

The method should return this:

1 5
3 1

6 2
6 5

6 3
3 8

9 4
4 2

The code I have only prints the 2D array into a grid. What I was thinking was to create 4 new grids in the method and then, with a series of forloops and if statements, print each new grid. that's why there are 4 2d arrays in the method. Any suggestions?

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]   " " );
            }
        }
    }
}

CodePudding user response:

You can return an array of grid that contains {topLeft, topRight, bottomLeft, bottomRight} like this.

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;
}

output:

[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]

CodePudding user response:

There are many ways to do this, one of them (and probably the safest!) of course being copy methods from the standard lib's Arrays class. However, if you are looking to develop your "manual" algorithmic skills with this one, then something like the following could be appropriate:

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();
}

This outputs:

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 
  • Related