我想從我的參考網絡中刪除一個人,我首先創建原始二維數組的副本,然後通過checkVertex ArrayList重新創建尺寸較小的2D網絡數組,checkVertex是我的唯一頂點列表。所以問題是,當重新填充size-1的新數組[] []是錯誤的,我不太清楚如何解決它。如何在Java中刪除二維數組中的特定行和列?
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)
這是什麼問題? –
請參閱http://stackoverflow.com/questions/8299771/copying-an-array-using-clone-original-array-being-changed/8299856#8299856瞭解有關在2D陣列上使用clone的信息。我認爲它不會回答你的整個問題,但它是必要的。 – ajb
不,我的重新填充新數組[1]的新數組[] []的方法是錯誤的,我不知道如何解決它。 –