Converting 'ArrayList to 'String[]' in Java

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

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

16条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 02:45

    In Java 11, we can use the Collection.toArray(generator) method. The following code will create a new array of strings:

    List list = List.of("one", "two", "three");
    String[] array = list.toArray(String[]::new)
    

    from java.base's java.util.Collection.toArray().

提交回复
热议问题