如何从二维数组中删除行? [英] How to remove a row from a 2d array?

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

问题描述

我有一个简单的数组,有点像这样:

I have a simple array, sort of like this:

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

因此,我们称其为 matrix [5] [9] .现在,我希望删除此矩阵中包含某个值(在本例中为 10 )的每一行,所以我只剩下...

So, let's call this matrix[5][9]. I wish to now remove every row within this matrix that contains a certain value, in this case 10, so I am left with...

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

推荐答案

以下是您可以运行的示例类,我相信它可以满足您的需求.从2D数组中删除行是一项棘手的工作,因为就像@KalebBrasee所说的那样,您不能真正地删除"已删除的行.它们,但是相反,您必须制作一个全新的2D数组.希望这会有所帮助!

Here's a sample class you can run that I believe does what you're looking for. Removing rows from 2D arrays is tricky business because like @KalebBrasee said, you can't really "remove" them, but rather you have to make a whole new 2D array instead. Hope this helps!

import java.util.ArrayList;
import java.util.List;

public class Matrix {
    private double[][] data;

    public Matrix(double[][] data) {
        int r = data.length;
        int c = data[0].length;
        this.data = new double[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                this.data[i][j] = data[i][j];
            }
        }
    }

    /* convenience method for getting a
       string representation of matrix */
    public String toString() {
        StringBuilder sb = new StringBuilder(1024);
        for (double[] row : this.data) {
            for (double val : row) {
                sb.append(val);
                sb.append(" ");
            }
            sb.append("\n");
        }

        return (sb.toString());
    }

    public void removeRowsWithValue(final double value) {
        /* Use an array list to track of the rows we're going to want to
           keep...arraylist makes it easy to grow dynamically so we don't
           need to know up front how many rows we're keeping */
        List<double[]> rowsToKeep = new ArrayList<double[]>(this.data.length);
        for (double[] row : this.data) {
            /* If you download Apache Commons, it has built-in array search
              methods so you don't have to write your own */
            boolean found = false;
            for (double testValue : row) {
                /* Using == to compares doubles is generally a bad idea
                   since they can be represented slightly off their actual
                   value in memory */
                if (Double.compare(value, testValue) == 0) {
                    found = true;
                    break;
                }
            }

            /* if we didn't find our value in the current row,
              that must mean its a row we keep */
            if (!found) {
                rowsToKeep.add(row);
            }
        }

        /* now that we know what rows we want to keep, make our
           new 2D array with only those rows */
        this.data = new double[rowsToKeep.size()][];
        for (int i = 0; i < rowsToKeep.size(); i++) {
            this.data[i] = rowsToKeep.get(i);
        }
    }

    public static void main(String[] args) {
        double[][] test = {
                {1, 2, 3, 4, 5, 6, 7, 8, 9},
                {6, 2, 7, 2, 9, 6, 8, 10, 5},
                {2, 6, 4, 7, 8, 4, 3, 2, 5},
                {9, 8, 7, 5, 9, 7, 4, 1, 10},
                {5, 3, 6, 8, 2, 7, 3, 7, 2}};

        //make the original array and print it out
        Matrix m = new Matrix(test);
        System.out.println(m);

        //remove rows with the value "10" and then reprint the array
        m.removeRowsWithValue(10);
        System.out.println(m);
    }
}

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

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