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,701
  • 5
  • 27
  • 46
Helisia
  • 548
  • 2
  • 6
  • 22

3 Answers3

8

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

Juvanis
  • 25,000
  • 3
  • 61
  • 84
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
  • 23,910
  • 10
  • 51
  • 62
0

Try the following code:

arraylist.toArray(array); // fill the array
hackjutsu
  • 6,728
  • 10
  • 39
  • 73
Ganesh Katikar
  • 2,460
  • 1
  • 21
  • 27