Search code examples
javaarrays2drowscol

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


If I have

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

How can i make all elements in row 0 have the value of "X" ? or all elements in row 1 have the value of "0" ?


Solution

  • You can do this using a for loop.

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

    You could put this in a function and pass the row and value

    public void standardiseRow(int row, int value)