Converting 'ArrayList to 'String[]' in Java

前端 未结 16 1493
情书的邮戳
情书的邮戳 2020-11-22 01:59

How might I convert an ArrayList object to a String[] array in Java?

16条回答
  •  清酒与你
    2020-11-22 02:32

    Starting from Java-11, one can alternatively use the API Collection.toArray(IntFunction generator) to achieve the same as:

    List list = List.of("x","y","z");
    String[] arrayBeforeJDK11 = list.toArray(new String[0]);
    String[] arrayAfterJDK11 = list.toArray(String[]::new); // similar to Stream.toArray
    

提交回复
热议问题