2

How I can convert ArrayList<String> to String[]? I'm tried to use this code:

ArrayList<String> al = getResources()...; //getting string-array from values.xml
String[] data = new String[al.size()];

for (int i = 0; i != al.size(); i++) {
    data[i] = al.get(i);
}

But it's very slow. How I can do it faster?

Zong
  • 5,920
  • 5
  • 28
  • 46
Helisia
  • 568
  • 2
  • 6
  • 22

3 Answers3

8

(String[]) al.toArray(); will do the trick.

Juvanis
  • 25,366
  • 5
  • 65
  • 85
6

Use ArrayList.toArray() method to convert list of string to array of string.

Do like this

String[] data = al.toArray(new String[al.size()]);
Prabhakaran Ramaswamy
  • 24,936
  • 10
  • 54
  • 63
0

Try the following code:

arraylist.toArray(array); // fill the array
hackjutsu
  • 7,564
  • 12
  • 43
  • 82
Ganesh Katikar
  • 2,532
  • 1
  • 23
  • 28