Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串列表中的所有值添加到单个字符串中?_Java - Fatal编程技术网

Java 如何将字符串列表中的所有值添加到单个字符串中?

Java 如何将字符串列表中的所有值添加到单个字符串中?,java,Java,restApplication.getPurposeOfLoans()是字符串类型的列表limit.getPurpose()是单个字符串值。我想将此列表中的所有字符串语句首尾相连地添加,并将它们设置为limit.setPurpose()语句。我该怎么做?我在下面分享了我的示例代码 Java示例 String pOfLoan = ""; for (int i = 0; i < restApplication.getPurposeOfTheLoans().size(); i++) {

restApplication.getPurposeOfLoans()
是字符串类型的列表
limit.getPurpose()
是单个字符串值。我想将此列表中的所有字符串语句首尾相连地添加,并将它们设置为
limit.setPurpose()
语句。我该怎么做?我在下面分享了我的示例代码

Java示例

String pOfLoan = "";
for (int i = 0; i < restApplication.getPurposeOfTheLoans().size(); i++) {
    pOfLoan.concat(restApplication.getPurposeOfTheLoans().get(i) + " ");
    limit.setPurpose(pOfLoan);
}
String pOfLoan=“”;
对于(int i=0;i
您可以使用流api
收集

pOfLoan = restApplication.getPurposeOfTheLoans().stream() // stream
              .collect(Collectors.joining(" ")); // join 
更好的选择:

pOfLoan = String.join(" ", restApplication.getPurposeOfLoans());
String pOfLoan=“”;

对于(int i=0;iBad idea,如果在
for
循环中执行此操作,最好使用字符串生成器。当我使用流api和join时,它是否会留下间隙?@okoreni是。请参阅
joining(“”)
。任何作为参数添加的内容都将用于连接。如果不需要任何内容,请不要指定任何内容。@okoreni
string.join(“”),restApplication.getPurposeOfLoans())
更整洁。
String pOfLoan = ""; 
for(int i=0;i<restApplication.getPurposeOfTheLoans().size();i++){ 
pOfLoan = pOfLoan.concat(restApplication.getPurposeOfTheLoans().get(i)+" "); 
}
limit.setPurpose(pOfLoan);