java通过替换数组值中的*来创建2D数组
处理二维数组时,必须使用“基于替换的开始”替换数组值
我的数组是[A B C D],它必须创建所有可能的组合,用数组中的*替换1、2、3和4个字符
例如
* B C D
A * C D
A B * D
A B C * //Replacing 1
* * C D
A * * D
A B * * //Replacing 2
* * * D
A * * *
* B * *
* * C * //Replacing 3
* * * * //Replacing 4
我编写的代码只是沿对角线方向更改值
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
// Filling the diagonals with second character
//if(i==j || (i+j)==(n-1))
if(i==j)
A[i][j] = c3;
else // Filling all other positions with second character
A[i][j] = c1;
}
}
for(int i=0; i<n/2; i++)
{
for(int j=i+1; j<m-1-i; j++)
{
// Filling the upper positions.
A[i][j] = c1;
// Filling the lower positions.
A[n-1-i][j] = c1;
}
}
// Printing the Matrix
System.out.println("\nOutput : \n");
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
需要帮忙吗
# 1 楼答案
这是工作代码,谢谢你的帮助
# 2 楼答案
以下是一个高度概括的版本:
这将使用行索引的位指定要替换的字符