【发布时间】:2021-12-19 18:29:21
【问题描述】:
我从一个格式化为useful.useless 的字符串数组开始。我需要修剪掉.useless 部分,然后将useful 部分放入二维char 数组中。
我看到了一种方法,它涉及创建仅包含 useful 部分的第二个字符串数组,然后将其转换为 2D 字符数组,但可能有更有效的方法。
但是,我不知道如何将字符串数组转换为二维字符数组
public static void main (String[]args){
// this here is just to create an array so the example runs
String in = "useful1.useless1,useful2.useless2,useful3.useless3,";
String[] strArray = null;
strArray = in.split(",");
/*the array of strings is thus
[useful1.useless1, useful2.useless2, useful3.useless3]
*/
char [][] charArray = new char [strArray.length][];
/* trim from the . so only useful part remains
then put in 2d char array, which should look like
[u,s,e,f,u,l,1]
[u,s,e,f,u,l,2]
[u,s,e,f,u,l,3]
the array will be a ragged array
*/
}
【问题讨论】:
-
您可以遍历数组,修改字符串,然后执行以下操作:stackoverflow.com/questions/36241039/…
-
这里有一个关于删除
.之后的所有内容的问题:stackoverflow.com/questions/12277461/… -
这看起来像家庭作业,我不认为这有什么其他用途。