gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:24:14 24 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 选择并返回多个数组元素?等等 arrayName.get(1 到 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926578/

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