在多维数组中替换一个字符串

huangapple go评论73阅读模式
英文:

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 = {
                { &quot;Oluchi&quot;, &quot;Mohammed&quot;, &quot;Kylo&quot;, &quot;Daniel&quot; },
                { &quot;Barry&quot;, &quot;Jonathan&quot;, &quot;Cylee&quot;, &quot;Themshni&quot; },
                { &quot;Jason&quot;, &quot;Ramazani&quot;, &quot;Anrich&quot;, &quot;Ashley&quot; },
                { &quot;Sianne&quot;, &quot;Blessing&quot;, &quot;Callum&quot;, &quot;Tyrone&quot; } };
        System.out.println(&quot;Printing Matrix:&quot;);
        for (int i = 0; i &lt; 4; i++) {
            for (int j = 0; j &lt; 4; j++) {
                System.out.print(original[i][j] + &quot; &quot;);
            }
            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 = {
{ &quot;Oluchi&quot;, &quot;Mohammed&quot;, &quot;Kylo&quot;, &quot;Daniel&quot; },
{ &quot;Barry&quot;, &quot;Jonathan&quot;, &quot;Cylee&quot;, &quot;Themshni&quot; },
{ &quot;Jason&quot;, &quot;Ramazani&quot;, &quot;Anrich&quot;, &quot;Ashley&quot; },
{ &quot;Sianne&quot;, &quot;Blessing&quot;, &quot;Callum&quot;, &quot;Tyrone&quot; } };
print(matrix);
System.out.println();
mask(matrix, 8, &quot;***&quot;);
print(matrix);
}
private static void print(String[][] matrix) {
for (int row = 0; row &lt; matrix.length; row++) {
for (int col = 0; col &lt; matrix[row].length; col++) {
if (col &gt; 0)
System.out.print(&#39; &#39;);
System.out.print(matrix[row][col]);
}
System.out.println();
}
}
private static void mask(String[][] matrix, int length, String str) {
for (int row = 0; row &lt; matrix.length; row++)
for (int col = 0; col &lt; 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

huangapple
  • 本文由 发表于 2020年10月20日 18:48:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64443581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定