Java 扫描二维数组中的下一个空值并替换行中的值

Java 扫描二维数组中的下一个空值并替换行中的值,java,arrays,multidimensional-array,nested-loops,Java,Arrays,Multidimensional Array,Nested Loops,因此,基本上,我需要搜索索引的下一个空值(指定为0),并用各种信息替换整行。例如,如果第三行中有一个空白元素,它将不是“0,0,0,0”,而是“(行号),a,b,c”。这就是我到目前为止所做的,我只是得到了一长串运行时错误 String[][] clientsArray = new String[20][4]; int rows = 20; int columns = 4; for (int r = 0; r < rows ; r++ ) { for (int c

因此,基本上,我需要搜索索引的下一个空值(指定为0),并用各种信息替换整行。例如,如果第三行中有一个空白元素,它将不是“0,0,0,0”,而是“(行号),a,b,c”。这就是我到目前为止所做的,我只是得到了一长串运行时错误

String[][] clientsArray = new String[20][4];
int rows = 20;
int columns = 4;

for (int r = 0; r < rows ; r++ )
    {
        for (int c = 0; c < columns ; c++ )
            {
                if (clientsArray[r][c].equals ("0"))
                {
                    String key = Integer.toString(r);
                    clientsArray[r][0] = key;
                    clientsArray[r][0+1] = "a"
                    clientsArray[r][0+2] = "b"
                    clientsArray[r][0+3] = "c"
                    break;
                }
             }
    }
String[][]clientsArray=新字符串[20][4];
int行=20;
int列=4;
对于(int r=0;r
目前,整个2d数组都充满了“0”,我只是没有包括这部分代码


**注意:我已将“c”值更改为0,从注释判断,您希望:

  • 搜索二维数组,查找第一列为“0”的第一行
  • 然后要替换该行中的每个元素

    String[][] clientsArray = new String[20][4];
    int rows = 20; // this can also be clientsArray.length
    int columns = 4; // this can also be clientsArray[0].length
    
    for (int r = 0; r < rows ; r++ )
    {
        //since you are only looking at the 1st column, you don't need the inner loop
           // this makes sure that the spot in the 2d array is set, otherwise trying to call .equals will crash your program.
           if (clientsArray[r][0] == null || clientsArray[r][0].equals ("0")) 
           {
              String key = Integer.toString(r);
              clientsArray[r][0] = key;
              clientsArray[r][1] = "a"
              clientsArray[r][2] = "b"
              clientsArray[r][3] = "c"   //you don't need the 0+, if you wanted to have a 2d array with more then 4 rows, you could put a for loop here insead of doing it 4 times like you did here
              break; //if you wanted to find ALL empty rows take this out.
              //also note if you have 2 loops like in your question, if would only break out of the 1st one
           }   
    }
    
    String[][]clientsArray=新字符串[20][4];
    int行=20;//这也可以是clientsArray.length
    int columns=4;//这也可以是clientsArray[0]。长度
    对于(int r=0;r

  • 对于初学者来说,实际上有20行,每行有4列。至少是您声明
    clientsArray
    的方式。当你修正这个问题时,如果
    c
    不是零,你认为当你使用例如
    c+3
    作为索引时会发生什么?这就是我想要的。当它搜索以0开头的行时,它会将该行的第一列替换为行号,然后将第二列替换为“a”,第三列替换为“b”,第四列替换为“c”,因为您不需要内部loop.omg。这简直要了我的命。干杯,伙计