gpt4 book ai didi

Java - 将一串数字按行读取到列中

转载 作者:行者123 更新时间:2023-12-02 05:26:00 25 4
gpt4 key购买 nike

我有一个这种形式的网格:

0 0 0 0 0 
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

我将以字符串形式接收它:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

这只是 5 x 5 网格的示例。但实际上,我有 20 列 x 15 行。网格中的值将不仅仅为零。

但是,我想知道如何将这样的一串数字逐行读入。我依靠这个字符串来使用 onDraw 方法检查 android GUI 中颜色的网格。我的方法逐行读取的地方。

感谢任何帮助!

最佳答案

没测试过。但我认为你需要类似的东西。拆分输入,每 20 个数字创建一个新的数组

String input = //put the input here

String[] inputSplitted = input.split(" "); //create array that holds all values.


int colSize = 20;

for(int i = 0; i < inputSplitted.length; i ++){
String[] row = new String[20]; //create array to hold a row
for(int j = i; j < i + row.length; j++)
row[j-i] = inputSplitted[j]; //add ekements the row

// do something with row

i += row.length; //increase i by row length so you add row.length + 1 index to the next row
}

请注意,现在每一行都是一个String[]。如果您想将数组的元素用作 int ,您必须执行 Integer.parseInt(row[i]); 等。

希望这有帮助。

如果您想将值存储在二维数组中,请在下面发表评论,我将更新我的答案。

关于Java - 将一串数字按行读取到列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25998446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com