如何从锯齿状的二维数组中删除行和列? [英] How do I remove a row and a column from a jagged 2d array?

查看:76
本文介绍了如何从锯齿状的二维数组中删除行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个 array ,看起来像这样:

Say I have an array that looks something like this:

{{1,3,5,7},{2,4,6,8,10,12},{2,3,5,7,11,13,17}}

我应该如何在Java中创建一个与此数组完全相同的新数组,除了删除了一行和一列之外?

How should I go about creating a new array in Java that is exactly the same as this one, except one row and one column has been removed?

我可以使用偶数大小的数组执行此任务,但是锯齿状的数组给我带来了一些麻烦.我考虑过先创建一个未指定列数的新数组,但是我从那里去哪里呢?

I can perform this task with an even-sized array, but jagged arrays are giving me some trouble. I thought about first creating a new array with unspecified column count, but where do I go from there?

/**
 * Creates a new array that is a copy of the input matrix,
 * except that one row and one column have been altered.
 * Precondition: the row index is between 0 (inclusive)
 * and the number of rows of matrix (not inclusive)
 *
 * @param matrix the input two dimensional array
 * @param row    the index of the row to remove
 * @param col    the index of the column to remove
 */
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
    int[][] altered = new int[(matrix.length - 1)][];
    int x = 0;
    for (int i = 0; i < matrix.length; i++) {
        if (matrix[i].length < col + 1 && i != row) {
            altered[x] = new int[matrix[i].length];
            for (int j = 0; j < altered[x].length; j++) {
                altered[x][j] = matrix[i][j];
            }
            if (x < matrix.length - 1) {
                x++;
            }
        } else if (matrix[i].length > col && i != row) {
            altered[x] = new int[matrix[i].length - 1];
            int y = 0;
            for (int z = 0; z < matrix[i].length - 1; z++) {
                if (z != col) {
                    altered[x][y] = matrix[i][z];
                    y++;
                } else {
                    z--;
                }
            }
            if (x < matrix.length - 1) {
                x++;
            }
        }
    }
    return altered;
}

在运行测试用例时,例如:

when running a test case such as:

removeRowAndCol(new int[][]{{1, 2}, {3, 4}}, 1, 1)

该方法返回正确的 {{1}} .

但是,这样的事情:

int[][] array = {{1,2,3,4},{11,12,13,14,15,16},{21,22,23,24},{31,32,33}};
removeRowAndCol(array, 0, 0)
removeRowAndCol(array, 2, 3)

该方法将冻结.

有人可以看一下代码,然后告诉我我做错了什么吗?

Can somebody take a look at the code and tell me what I did wrong?

推荐答案

一个二维数组,无论是否呈锯齿状,都是比其他数组更复杂的数组.您必须手动创建每一行,因此,您可以为每一行选择任意大小.

A bidimensional array, jagged or not, is more an array of array than anything else. You have to create each row by hand and thus, you can choose any size for each individual row.

import java.util.Arrays;

public class Temp {
    public static void main(String[] args) {
        int[][] jagged = {{1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16}};
        System.out.println("Jagged: " + Arrays.deepToString(jagged));
        System.out.println("Smaller 1: " + Arrays.deepToString(removeRowAndCol(jagged, 0, 0)));
        System.out.println("Smaller 2: " + Arrays.deepToString(removeRowAndCol(jagged, 1, 1)));
        System.out.println("Smaller 3: " + Arrays.deepToString(removeRowAndCol(jagged, 2, 2)));
    }

    private static int[][] removeRowAndCol(int[][] jagged, int i, int j) {
        int[][] smaller = new int[jagged.length - 1][];

        // WARN: outofbounds checks are not implemented!
        for (int smallerI = 0; smallerI < smaller.length; smallerI++) {
            int sourcedI = smallerI;
            if (smallerI >= i) {
                sourcedI++;
            }

            smaller[smallerI] = new int[jagged[sourcedI].length - 1];

            for (int smallerJ = 0; smallerJ < smaller[smallerI].length; smallerJ++) {
                int sourcedJ = smallerJ;
                if (smallerJ >= j) {
                    sourcedJ++;
                }
                smaller[smallerI][smallerJ] = jagged[sourcedI][sourcedJ];
            }
        }

        return smaller;
    }
}

哪个输出:

Jagged: [[1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
Smaller 1: [[5, 6, 7, 8], [10, 11, 12, 13, 14, 15, 16]]
Smaller 2: [[1, 3], [9, 11, 12, 13, 14, 15, 16]]
Smaller 3: [[1, 2], [4, 5, 7, 8]]

这篇关于如何从锯齿状的二维数组中删除行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆