public class test9 {
public static void main(String[] args) {
int[][]a={
{1,2,3,4},
{5,6,7,8},
{9,0,11,12},
{13,14,15,16},
{101,102,103,104}
};
int[] RowRec=new int[a.length]; //记录原数组哪行有0
int[] ColRec=new int[a[0].length]; //记录原数组哪列有0
for (int i = 0; i <a.length ; i++) {
for (int j = 0; j <a[0].length ; j++) {
if (a[i][j]==0){ //遍历数组,若原数组有0,则将0所在的行列记录下来
RowRec[i]=1; //记录行存在0
ColRec[j]=1; //记录列存在0
}
}
}
for (int i = 0; i <a.length ; i++) {
for (int j = 0; j <a[0].length ; j++) {
if (RowRec[i]==1||ColRec[j]==1){ //行或列出现0则将该行和该列置零
a[i][j]=0;
}
}
}
util.print2Darr(a);
}
}
将数组中0所在的行和列清零
最新推荐文章于 2024-07-17 23:24:38 发布