简体   繁体   中英

Making a 2D array from 1D arrays while reading a txt file

So I am having a file containing a matrix with a defined number of rows and columns, which I am trying to fill in a 2D array. And I am not sure how to proceed to copy the lines that are being read into my 2D array.

    String str;
    char[][] m = new char[16][16];
    while((s = in.readLine()) != null) {
        char[] row = s.toCharArray();
        /* ??? */
    }       
    in.close();

Maybe something like this?

String str;
char[][] m = new char[16][16];
int col = 0;
while((s = in.readLine()) != null) {
    char[] row = s.toCharArray();
    for (int i = 0; i < 16; i++) {
        m[col][i] = row[i];
    }
    col++;
}
in.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM