如何给某行元素赋予相同的值?使用二维数组?

How can I give a certain row of elements all the same value? Using a 2d array?

如果我有

String[][] trenches = new String[10][10];

如何使第 0 行中的所有元素都具有 "X" 的值? 或第 1 行中的所有元素的值为“0”?

试试这个代码:

String[][] trenches = new String[10][10];

trenches[0] = StringUtils.repeat("X", 10).split("");
trenches[1] = StringUtils.repeat("0", 10).split("");

将二维数组视为 x 和 y 坐标系。你可以有一个双 for 循环,多次通过你的 x,但只通过你的 y 一次。在这种情况下,您的外循环将是 y,您的内循环将是 x。

for (int y = 0; y < array.length; y++) {
    for (int x = 0; x < array.length; x++) {
        if (y == 0) {             
            array[x][y] = X;
        }
        if (y == 1) {
           array[x][y] = 0;
        } 
    }
} 

您可以使用 for 循环执行此操作。

int row = 0;
int value = 5;

for(int i = 0; i < trenches[row].length(); i ++)
{
    tenches[row][i] = value;
}

您可以将其放入函数中并传递行和值

public void standardiseRow(int row, int value)