Import java. util. arraylist;
Import java. util. List;
Public class matrixdemo {
// Find all sets of zero elements in the two-dimensional matrix
Public static list <posiztion> findzero (int A [] []) {
List <posiztion> List = new arraylist <posiztion> ();
Int ROW = A. length;
Int Col = A [0]. length;
For (INT I = 0; I <row; I ++ ){
For (Int J = 0; j <Col; j ++ ){
If (A [I] [J] = 0 ){
Posiztion P = new posiztion ();
P. setx (I );
P. sety (j );
List. Add (P );
}
}
}
Return list;
}
// Replace all rows and columns of the element 0 in the matrix with 0
Public static int [] [] replacezero (int A [] []) {
Int B [] [] =;
List <posiztion> L = findzero ();
For (INT I = 0; I <L. Size (); I ++ ){
Posiztion P = L. Get (I );
Int x = P. getx ();
Int y = P. Gety ();
For (Int J = 0; j <A. length; j ++ ){
B [J] [Y] = 0;
}
For (int K = 0; k <A [0]. length; k ++ ){
B [x] [k] = 0;
}
}
Return B;
}
// Print the functions of the two-dimensional matrix
Public static void printmatrix (int A [] []) {
For (INT I = 0; I <A. length; I ++ ){
For (Int J = 0; j <A [0]. length; j ++ ){
System. Out. Print (A [I] [J] + "");
}
System. Out. println ();
}
}
Public static void main (string [] ARGs ){
Int A [] [] = {1, 2, 3, 4, 5}, {1, 2, 3, 4, 0}, {0, 1, 2, 3, 4}, {1, 2, 3, 4, 5 }};
List <posiztion> L = findzero ();
System. Out. println ("the position of zero are :");
For (INT I = 0; I <L. Size (); I ++ ){
System. Out. println ("(" + L. Get (I). getx () + "," + L. Get (I). Gety ()
+ ")");
}
System. Out. println ("before Replace the matrix is :");
Printmatrix ();
System. Out. println ("after replace the matrix is :");
Int B [] [] = replacezero ();
Printmatrix (B );
}
}
// Define a class to store the position of the 0 element. X indicates the row in which the column is located and Y indicates the column in which the element is located.
Class posiztion {
Private int X;
Private int y;
Public int getx (){
Return X;
}
Public void setx (int x ){
This. x = X;
}
Public int Gety (){
Return y;
}
Public void sety (INT y ){
This. Y = y;
}
}
Running result:
The position of zero are:
(1, 4)
(2, 0)
Before Replace the matrix is:
1 2 3 4 5
1 2 3 4 0
0 1 2 3 4
1 2 3 4 5
After Replace the matrix is:
0 2 3 4 0
0 0 0 0 0
0 0 0 0 0
0 2 3 4 0