Java - 使用字符串填充数组

时间:2015-03-28 18:17:19

标签: java arrays string character

我想填充未知长度的字符串,将其拆分为字符并将其放入数组中,重复此过程直到数组已满。这是我使用的多维数组。

    key = input from user as string (length is unknown)
    int[][] fullKey = new int[128][2];
    do {
        for (int ii = 0; ii < key.length(); ii++) {
            fullKey[i][0] = key.charAt(ii);
            i++;
        }
    } while (fullKey[encoding][0] == null);

我知道int是原始的,所以不能为空,但它只是试图解释我试图做的事情

提前致谢

1 个答案:

答案 0 :(得分:0)

以下代码是否符合您的要求?

String key = "string";
int row = 2;
int col = 128;
int[][] fullKey = new int[row][col];
int counter=0;

for (int i = 0; i < col; i++) {
    fullKey[0][i] = key.charAt(counter);
    counter = (counter==key.length()-1) ? 0 : (counter+1);
}