Converting 'ArrayList to 'String[]' in Java

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

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

16条回答
  •  温柔的废话
    2020-11-22 02:44

    You can use Iterator to iterate the elements of the ArrayList:

    ArrayList list = new ArrayList<>();
    String[] array = new String[list.size()];
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext(); i++) {
        array[i] = iterator.next();
    }
    

    Now you can retrive elements from String[] using any Loop.

提交回复
热议问题