选择并返回多个数组元素?等等 arrayName.get(1 到 3)

我正在尝试格式化一个字符串,每个单词之间的空格数需要根据每个字符串进行更改。我已经完成了计算所需空格数量的代码。我创建了一个包含多个“”元素的数组,我用这些元素来更改每个单词之间的空格数。如何打印数组中的 X 到 Y 元素?


blankarrayList.get(1-2) // just prints the item in position -1

blankarrayList.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);

}   


梵蒂冈之花
浏览 107回答 1
1回答

人到中年有点甜

blankarrayList.get(/*array&nbsp;positions&nbsp;1&nbsp;through&nbsp;3*/) blankarrayList.subList(1,&nbsp;3);&nbsp;//&nbsp;This&nbsp;gives&nbsp;a&nbsp;sub-list&nbsp;which&nbsp;specified&nbsp;index因此,您可以将新子列表中的所有元素组合起来,如下所示System.out.println(myWords.get(0)&nbsp;+&nbsp;StringUtils.join(blankarrayList.subList(1,&nbsp;3),&nbsp;"")&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;myWords.get(1)&nbsp;+&nbsp;StringUtils.join(blankarrayList.subList(1,&nbsp;3),&nbsp;"") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;myWords.get(2)&nbsp;+&nbsp;StringUtils.join(blankarrayList.subList(1,&nbsp;3),&nbsp;"") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;my&nbsp;words.get(3));您也可以像下面这样编写所有内容:System.out.println(StringUtils.join(myWords,&nbsp;blankarrayList.subList(1,&nbsp;3)));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java