我认为已经有人问过这个问题了,但仍然如此。
我有随机字符串值的 ArrayList,我需要将这些值放入另一个 ArrayList,但只放在一个索引中。
示例:
我有
{ "hey","how","are","you"}
我需要做到
{"Hey how are you"}
是否有某种命令,例如 join 或 smth?因为我尝试向同一索引添加另一个字符串值,但它只是替换了它。
最佳答案
您可以使用 StringJoiner 来实现该解决方案。
List<String> mainList = new ArrayList<>(); //The one to contain all Strings from yourList
List<String> yourList = Arrays.asList("Hey", "how", "are", "you"); //The list of Strings
StringJoiner joiner = new StringJoiner(" "); //Space as delimiter, strings will be separated with spaces
for (String s : yourlist) {
joiner.add(s);
}
mainList.add(joiner.toString()); //Add the new concatenated String to your list, in a single string
关于java - 数组列表。在一个索引中连接字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542035/