How to concat string values in array list

前端 未结 2 1618
你的背包
你的背包 2021-01-29 11:43

I need to print all arraylist values at a time using concat.

Here is my code:

ArrayList lst = new ArrayList();
lst.add(\"hi\"         


        
2条回答
  •  Happy的楠姐
    2021-01-29 12:40

    Please prefer the List interface to the ArrayList concrete type. Assuming you are using Java 8+, you might use a Stream and Collectors.joining (and Arrays.asList) like

    List lst = Arrays.asList("hi", "hello");
    String r = lst.stream().collect(Collectors.joining(" "));
    System.out.println(r);
    

    Which outputs

    hi hello
    

    As requested.

提交回复
热议问题