1

I want to remove a person from my reference web and I first create a copy of the original 2D array and then I remake the 2D web array with a size smaller via the checkVertex ArrayList, checkVertex is a list of my unique vertices. So the problem is that when repopulating my new array[][] of size-1 is wrong, and I'm not quite sure how to fix it.

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)
3
  • And what is the question? Commented Oct 15, 2013 at 21:36
  • Please see stackoverflow.com/questions/8299771/… for information on using clone on 2D arrays. It won't answer your entire question, I think, but it is necessary. Commented Oct 15, 2013 at 21:39
  • No, my method of repopulating the new array[][] of a new size-1, is wrong and I don't know quite how to fix it. Commented Oct 16, 2013 at 3:30

2 Answers 2

1

You cannot change the dimension of an existing Array data-structure unless you destroy and rebuild it with the new dimension (you have to re-initialise and re-populate).

Edited :

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

Why +2 ? Is the new matrix bigger than the original ? Can't be.

Look at the end of this program to see how it can be done..Hope it helps.

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


            }

        }
Sign up to request clarification or add additional context in comments.

1 Comment

Check the edit. I have posted a working algo to solve your problem.
0
int n = matrixcopy.length;
int m = matrixcopy[0].length
int[][] matrix = new int[n-1][m-1];
for(int i=0 ; i<n-1 ; i++){
    for(int j=0 ; j<m-1 ; j++){
        matrix[i][j] = matrixcopy[i+(i>=rowToDelete?1:0)][j+(j>=colToDelete?1:0)];
    }
}`

1 Comment

Please add some explanation as showing only code might not help at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.