簡體   English   中英

掃描二維數組中的下一個空值,並替換行中的值

[英]scanning for next empty value in a 2d array and replacing values in the row

因此,基本上,我需要搜索索引的下一個空值(指定為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;
                }
             }
    }

目前,整個2d數組都填充有0,我只是沒有包括該部分代碼。

**注意:我已將“ c”的值更改為0

從評論來看,您正在尋找:

  1. 搜索2D數組,查找第一列為“ 0”的第一行。
  2. 然后,您要替換該行中的每個元素。

     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 } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM