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
  • 6,160
  • 5
  • 32
  • 46
Helisia
  • 568
  • 2
  • 7
  • 23

3 Answers3

8

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

Juvanis
  • 25,802
  • 5
  • 69
  • 87
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
  • 25,706
  • 10
  • 57
  • 64
0

Try the following code:

arraylist.toArray(array); // fill the array
hackjutsu
  • 8,336
  • 13
  • 47
  • 87
Ganesh Katikar
  • 2,620
  • 1
  • 26
  • 28