英文:
Replace a string in a multidimensional Array
问题
下面是我的代码。我需要将矩阵中任何长度为8个字符的单词替换为"***"。替换数组中的单词的最简单方法是什么?
public class Array {
public static void main(String[] args) {
String[][] original = {
{ "Oluchi", "Mohammed", "Kylo", "Daniel" },
{ "Barry", "Jonathan", "Cylee", "Themshni" },
{ "Jason", "Ramazani", "Anrich", "Ashley" },
{ "Sianne", "Blessing", "Callum", "Tyrone" } };
System.out.println("Printing Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (original[i][j].length() == 8) {
original[i][j] = "***";
}
System.out.print(original[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
请注意,我已经添加了代码以在矩阵中找到长度为8的单词并将其替换为"***"。
英文:
Below is my code that I have. I need to replace any words of the matrix that is 8 characters long to be "***". What is the most simplest way to replace the words in the array.
public class Array {
public static void main(String[] args) {
String[][] original = {
{ "Oluchi", "Mohammed", "Kylo", "Daniel" },
{ "Barry", "Jonathan", "Cylee", "Themshni" },
{ "Jason", "Ramazani", "Anrich", "Ashley" },
{ "Sianne", "Blessing", "Callum", "Tyrone" } };
System.out.println("Printing Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(original[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
答案1
得分: 0
public class Array {
public static void main(String[] args) {
String[][] matrix = {
{ "Oluchi", "Mohammed", "Kylo", "Daniel" },
{ "Barry", "Jonathan", "Cylee", "Themshni" },
{ "Jason", "Ramazani", "Anrich", "Ashley" },
{ "Sianne", "Blessing", "Callum", "Tyrone" }
};
print(matrix);
System.out.println();
mask(matrix, 8, "***");
print(matrix);
}
private static void print(String[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (col > 0)
System.out.print(' ');
System.out.print(matrix[row][col]);
}
System.out.println();
}
}
private static void mask(String[][] matrix, int length, String str) {
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
if (matrix[row][col].length() == length)
matrix[row][col] = str;
}
}
Output:
Oluchi Mohammed Kylo Daniel
Barry Jonathan Cylee Themshni
Jason Ramazani Anrich Ashley
Sianne Blessing Callum Tyrone
Oluchi *** Kylo Daniel
Barry *** Cylee ***
Jason *** Anrich Ashley
Sianne *** Callum Tyrone
英文:
public class Array {
public static void main(String[] args) {
String[][] matrix = {
{ "Oluchi", "Mohammed", "Kylo", "Daniel" },
{ "Barry", "Jonathan", "Cylee", "Themshni" },
{ "Jason", "Ramazani", "Anrich", "Ashley" },
{ "Sianne", "Blessing", "Callum", "Tyrone" } };
print(matrix);
System.out.println();
mask(matrix, 8, "***");
print(matrix);
}
private static void print(String[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (col > 0)
System.out.print(' ');
System.out.print(matrix[row][col]);
}
System.out.println();
}
}
private static void mask(String[][] matrix, int length, String str) {
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
if (matrix[row][col].length() == length)
matrix[row][col] = str;
}
}
Output:
Oluchi Mohammed Kylo Daniel
Barry Jonathan Cylee Themshni
Jason Ramazani Anrich Ashley
Sianne Blessing Callum Tyrone
Oluchi *** Kylo Daniel
Barry *** Cylee ***
Jason *** Anrich Ashley
Sianne *** Callum Tyrone
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论