Converting List to String[] in Java

后端 未结 6 1631
暖寄归人
暖寄归人 2020-12-25 10:52

How do I convert a list of String into an array? The following code returns an error.

public static void main(String[] args) {
    List strlist         


        
6条回答
  •  旧巷少年郎
    2020-12-25 11:18

    You want

    String[] strarray = strlist.toArray(new String[0]);
    

    See here for the documentation and note that you can also call this method in such a way that it populates the passed array, rather than just using it to work out what type to return. Also note that maybe when you print your array you'd prefer

    System.out.println(Arrays.toString(strarray));
    

    since that will print the actual elements.

提交回复
热议问题