2013-10-15 4 views
1

참조 웹에서 사람을 제거하고 싶습니다. 먼저 원본 2D 배열 복사본을 만든 다음 checkVertex ArrayList를 통해 크기가 더 작은 2D 웹 배열을 다시 작성합니다. checkVertex는 고유 한 정점 목록입니다. 그래서 문제는 크기 -1의 새 배열을 다시 채울 때 잘못 되었기 때문에 그것을 고칠 방법이 확실하지 않다는 것입니다.Java에서 2D 배열의 특정 행과 열을 삭제하는 방법은 무엇입니까?

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) 
+0

그리고 무엇이 문제입니까? –

+0

2D 배열에서 '복제'사용에 대한 정보는 http://stackoverflow.com/questions/8299771/copying-an-array-using-clone-original-array-being-changed/8299856#8299856을 참조하십시오. 그것은 당신의 전체 질문에 대답하지 않을 것이라고 생각합니다. 그러나 그것은 필요합니다. – ajb

+0

아니요, 새 배열 [] []을 (를) 새 크기 -1로 다시 채우는 제 방법은 잘못되었으며이를 수정하는 방법을 잘 모릅니다. –

답변

1

당신은 파괴하지 않는 한 기존의 배열 데이터 구조의 차원을 변경하고 새로운 차원으로 재 구축 할 수는 (당신은-초기화를 다시하고 다시 채우기를해야합니다).

편집 :

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


      } 

     } 
+0

그래, 내 코드에서 그걸하고있다 ... –

+0

편집을 확인하십시오. 나는 당신의 문제를 해결하기 위해 일하는 골판지를 올렸다. –

관련 문제