java - 如何在Java中删除二维数组中的特定行和列?

标签 java multidimensional-array

我想从我的引用网络中删除一个人,我首先创建原始 2D 数组的副本,然后通过 checkVertex ArrayList 重新制作尺寸更小的 2D Web 数组,checkVertex 是我的唯一顶点的列表。所以问题是,当重新填充 size-1 的新 array[][] 时是错误的,而且我不太确定如何修复它。

public static void deletePerson(String name)
{
    //checkVertex is a list of the unique vertices
    int rowNum = 0;
    int colNum = 0;
    int origRows = checkVertex.size() + 1; //+1 is for the [0][0] null spot
    int origCols = checkVertex.size() + 1;
    String person = name;
    String[][] copy = matrix.clone();

    for(int x=0; x<checkVertex.size() + 1; x++)
    {
        if( matrix[x][0] == null )
        {
            //do nothing
        }
        else if( matrix[x][0].equalsIgnoreCase(person) )
        {
            rowNum = x;
            break;
        }
    }
    for(int z=0; z<checkVertex.size() + 1; z++)
    {
        if( matrix[0][z] == null )
        {
            //do nothing
        }
        else if( matrix[0][z].equalsIgnoreCase(person) )
        {
            colNum = z;
            break;
        }
    }


    //Now remove them from the list of vertexes
    for(int i=0; i<checkVertex.size(); i++)
    {
        if(checkVertex.get(i).equalsIgnoreCase(person))
        {
            checkVertex.remove(i);
            break;
        }
    }

    setNum(checkVertex.size());

    //Build the sides of the matrix
    //Starting with the first col
    matrix = new String[checksSize + 1][checksSize + 1];

    for(int x = 0 ; x < checksSize ; x++)
    {
        String vertice = checkVertex.get(x);
        if( x == rowNum )
        {
            continue;
        }
        else
        {
            matrix[x+1][0] = vertice;
        }
    }

    //Now get the top row
    for(int x = 0 ; x < checksSize ; x++)
    {
        String vertice = checkVertex.get(x);
        if( x == colNum )
        {
            continue;
        }
        else
        {
            matrix[0][x+1] = vertice;
        }
    }

    //Now fill in the references
    for(int i=1; i<checkVertex.size() + 2; i++)
    {
        if( i == rowNum )
        {
            continue;
        }
        else
        {
            for(int j=1; j<checkVertex.size() + 2; j++)
            {
                if( j == colNum )
                {
                    //continue;
                    matrix[i][j-1] = copy[i][j];
                    j++;
                }
                else
                {
                    matrix[i][j] = copy[i][j];
                }
            }//end for j
        }
    }//end for i

}//END deletePerson(String name)

最佳答案

您无法更改现有数组数据结构的维度,除非您销毁它并使用新维度重建它(您必须重新初始化并重新填充)。

已编辑:

 for(int i=1; i<checkVertex.size() + 2; i++)

为什么+2?新的矩阵比原来的矩阵大吗?不可能。

看看这个程序的结尾,看看它是如何完成的。希望它有所帮助。

    public static void main(String[] args) {

        int rows=5;
        int cols=5;
        double[][] matrix = new double[rows][cols];

        int counter =0;

        for(int i=0; i<rows; i++)
        for(int j=0; j<cols; j++){

            matrix[i][j] = Math.random();
             // counter++;
           // System.out.println("First test ("+counter+") : " + matrix[i][j]);

        }

        //keep copy of original matrix
        double[][] matrixCopy = matrix.clone();

        //Assume 
        int rowToRemove = 2;
        int colToRemove = 3;


        // re-initialise matrix with dimension i-1 , j-1
        matrix = new double[rows-1][cols-1];
        counter = 0;

        //row and column counter for the new matrix
        int tmpX=-1;
        int tmpY=-1;


        //re-populate new matrix by searching through the original copy of matrix, while skipping useless row and column
        // works only for 1 row and 1 column in a 2d array but by changing the conditional statement we can make it work for n number of rows or columns in a 2d array.
        for(int i=0; i<rows; i++)
        {
         tmpX++;
         if(i==rowToRemove){
             tmpX--;
         }
         tmpY=-1;
            for(int j=0; j<cols; j++){


               tmpY++;
              if(j==colToRemove){
              tmpY--;
              }

                 if(i!=colToRemove&&j!=colToRemove){
                       counter++;
                   matrix[tmpX][tmpY] = matrixCopy[i][j];

                   System.out.println(counter+" :"+matrix[tmpX][tmpY]);
                 }


            }

        }

关于java - 如何在Java中删除二维数组中的特定行和列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391346/

相关文章:

json - jq:获取二维数组的维度

java - 在 Java 中尝试 Throw Catch

java - 模态对话框

java - 堆栈跟踪打印抑制

java - 在 Java 中创建二维数组的语法

c - 为什么 char arr[][] 类型与 char** arr 不兼容?以及如何修复它

java - 以编程方式设置数据源的用户名和密码

java - 单击下一步播放新的音轨

C++:如何创建多类型和多维数组格式?

java - 如何使用特定值填充 3d 数组