如何删除二维数组中的行

Ale*_*exT 5 java multidimensional-array

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

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
Run Code Online (Sandbox Code Playgroud)

所以,我们称之为matrix[5][9].我希望现在删除此矩阵中包含特​​定值的每一行,在本例中为10,所以我留下......

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
Run Code Online (Sandbox Code Playgroud)

Bre*_*ode 8

这是一个你可以运行的示例类,我相信你正在寻找什么.从2D数组中删除行是很棘手的事情,因为像@KalebBrasee所说,你不能真正"删除"它们,而是你必须改造一个全新的2D数组.希望这可以帮助!

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Krz*_*Goj 5

使用使用而不是数组.有快速访问随机元素和慢速方法,它是相反的.你必须自己选择.System.arraycopy java.util.ListArrayListremoveLinkedList

  • 我想你只需要将%20放入空间 (2认同)