简体   繁体   中英

how to convert string abcdefghijklmnop to a 4x4 matrix in java?

I am trying to convert a string to a matrix of 4x4 size. The matrix should contain the elements same as that of the string. For eg.

String="1234567890111213141516";

I need to convert this to a 4x4 matrix in java.

a[0][0]=1;
a[0][1]=2;

and so on. Can anyone suggest me the code for the same?

Here is a hint:

a[i][j] = Integer.valueOf(str.charAt(4 * i + j));

Figuring out how to use this to populate the entire matrix is left as an exercise for the reader.

PS This will work for a 16-character abcdefghijklmnop string as in your title; if the string is longer, as in the question, there is no unambiguous way to parse it into a 4x4 matrix.

PPS If all you need to do is set the elements to the consecutive numbers from 1 to 16, you don't need the string at all.

Indeed your question is an easy one, so I answer your question with some additional points that I think help you more than the answer itself:

First, this is the code:

int N = 4;
String delim = ",";
String s = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16";
String[] c = s.split(delim);
int[][] mat = new int[4][4];
int l = 0;
for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++,l++)
        mat[i][j] = Integer.valueOf(c[l]);

Notes:
Please
1 - don't a simple problem a real hard one!

if it's a hw you can convince the tutor about defining the string delimited with something like "," (if he/she insists) or whatever, and if this is a real project convince the customer, you don't want to torture yourself!

String s = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16";

2 - you said you want to split the string into a 4 x 4 matrix, what if you want a 5x5 matrix? So take advantage of constants, and the same for the delimiter

int N = 4;
String delim = ",";

Hope these help.

You should run two for loops through your string and fill up the matrix. (This can be done with one array too, but let's keep it simple.)

    String str ="1234567890111213141516";
    final int rowSize = 4;
    final int columnSize = 4;
    String[][] a = new String[rowSize][columnSize];
    // iterate
    for (int row = 0; row < rowSize; row++) {
        for (int column = 0; column < columnSize; column++) {
            a[row][column] = String.valueOf(str.charAt(rowSize * row + column));
        }
    }
    // test
    for (int row = 0; row < rowSize; row++) {
        for (int column = 0; column < columnSize; column++) {
            System.out.print(a[row][column] + " ");
        }
        System.out.println();
    }

For reading a String s = "1234567890abcdefghij" , you can add below code snippet :

char[][] a = new char[4][4];

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
        a[i][j] = s.charAt(4 * i + j);
    }
}

this will help you. This will work for only one digit character only.

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