Convert ArrayList to String array in Android

前端 未结 7 1171
终归单人心
终归单人心 2020-12-08 00:56

I have a ArrayList that contains values in the form [ann,john]. I want to convert this ArrayList into a String array in the form {\"ann\",\"john\"}.

How should I do

相关标签:
7条回答
  • 2020-12-08 01:37

    Well in general:

    List<String> names = new ArrayList<String>();
    names.add("john");
    names.add("ann");
    
    String[] namesArr = new String[names.size()];
    for (int i = 0; i < names.size(); i++) {
        namesArr[i] = names.get(i);  
    }
    

    Or better yet, using built in:

    List<String> names = new ArrayList<String>();
    String[] namesArr = names.toArray(new String[names.size()]);
    
    0 讨论(0)
提交回复
热议问题