我正在尝试格式化一个字符串,每个单词之间的空格数需要根据每个字符串进行更改。我已经完成了计算所需空格数量的代码。我创建了一个包含多个“”元素的数组,我用这些元素来更改每个单词之间的空格数。如何打印数组中的 X 到 Y 元素?blankarrayList.get(1-2) // just prints the item in position -1blankarrayList.get(1,2) // just prints items one and two, this works but // doesnt allow me to easily change the number printed (that i know of)public static String format(List<String> myWords) { System.out.println(myWords.get(0) + blankarrayList.get(/*array positions 1 through 3*/) + myWords.get(1) + blankarrayList.get(/*array positions 1 through 3*/) + myWords.get(2) + blankarrayList.get(/*array positions 1 through 3*/) + my words.get(3)); return myWords.get(0);}
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
blankarrayList.get(/*array positions 1 through 3*/) blankarrayList.subList(1, 3); // This gives a sub-list which specified index
因此,您可以将新子列表中的所有元素组合起来,如下所示
System.out.println(myWords.get(0) + StringUtils.join(blankarrayList.subList(1, 3), "") + myWords.get(1) + StringUtils.join(blankarrayList.subList(1, 3), "") + myWords.get(2) + StringUtils.join(blankarrayList.subList(1, 3), "") + my words.get(3));
您也可以像下面这样编写所有内容:
System.out.println(StringUtils.join(myWords, blankarrayList.subList(1, 3)));
添加回答
举报
0/150
提交
取消