我试图按照标题所说的那样做,但我目前得到了错误的输出,我在这段代码中做错了什么?public void AAAAA(String thetext, int numb) { List<String> items = new LinkedList<String>(Arrays.asList(thetext.split(" "))); //split string to individual characters int len = items.size(); //get size of string for(int j = 0; j < len; j++){ // loop through list if(j % width == 0){ items.add(numb, "\n"); } } System.out.println(items); }}文本示例:“你好,我叫鲍勃,我喜欢抚摸可爱的肥狗”预期输出如果numb是 3;hello my nameis bob ilike to petcute fat dogs
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
尝试结合使用 modula 和附加到新列表
String thetext = "hello my name is bob i like to pet cute fat dogs";
int splitOn = 3;
String arr [] = thetext.split(" ");
List<String> newList = new ArrayList<String>();
StringBuilder buf = new StringBuilder();
for (int x = 0; x < arr.length; x++) {
buf.append(arr[x]).append(" ");
if ((x + 1) % splitOn == 0) {
newList.add(buf.toString());
newList.add(" ");
buf = new StringBuilder();
}
}
newList.add(buf.toString());
for (String line : newList) {
System.out.println(line);
}
添加回答
举报
0/150
提交
取消