Home » Java » Converting 'ArrayList<String> to 'String[]' in Java

Converting 'ArrayList<String> to 'String[]' in Java

Posted by: admin November 2, 2017 Leave a comment

Questions:

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

Answers:
List<String> list = ..;
String[] array = list.toArray(new String[0]);

For example:

List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);

The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.

Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.

Questions:
Answers:

An alternative in Java 8:

String[] strings = list.stream().toArray(String[]::new);

Questions:
Answers:
ArrayList<String> arrayList = new ArrayList<String>();
Object[] objectList = arrayList.toArray();
String[] stringArray =  Arrays.copyOf(objectList,objectList.length,String[].class);

Using copyOf, ArrayList to arrays might be done also.

Questions:
Answers:

You can use the toArray() method for List:

ArrayList<String> list = new ArrayList<String>();

list.add("apple");
list.add("banana");

String[] array = list.toArray(new String[list.size()]);

Or you can manually add the elements to an array:

ArrayList<String> list = new ArrayList<String>();

list.add("apple");
list.add("banana");

String[] array = new String[list.size()];

for (int i = 0; i < list.size(); i++) {
    array[i] = list.get(i);
}

Hope this helps!

Questions:
Answers:

In Java 8:

String[] strings = list.parallelStream().toArray(String[]::new);

Questions:
Answers:

in case some extra manipulation of the data is desired, for which the user wants a function, this approach is not perfect (as it requires passing the class of the element as second parameter), but works:

import java.util.ArrayList;
import java.lang.reflect.Array;

public class Test {
  public static void main(String[] args) {
    ArrayList<Integer> al = new ArrayList<>();
    al.add(1);
    al.add(2);
    Integer[] arr = convert(al, Integer.class);
    for (int i=0; i<arr.length; i++)
      System.out.println(arr[i]);
  }

  public static <T> T[] convert(ArrayList<T> al, Class clazz) {
    return (T[]) al.toArray((T[])Array.newInstance(clazz, al.size()));
  }
}

Questions:
Answers:

Generics solution to covert any List<Type> to String []:

public static  <T> String[] listToArray(List<T> list){
    String [] array = new String[list.size()];
    for (int i =0 ; i<array.length;i++)
        array[i] = list.get(i).toString();
    return array;
}

Note You must override toString() method.

class Car{
 private String name;
  public Car(String name){
    this.name = name;
  }
 public String toString(){
   return name;
 }
}
final List<Car> carList = new ArrayList<Car>();
carList.add(new Car("BMW"))
carList.add(new Car("Mercedes"))
carList.add(new Car("Skoda"))
final String[] carArray = listToArray(carList);

Questions:
Answers:
List <String> list = ...
String[] array = new String[list.size()];
int i=0;
for(String s: list){
  array[i++] = s;
}

Questions:
Answers:

You can use Iterator<String> to iterate the elements of the ArrayList<String>

Code :

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

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

Questions:
Answers:

By using toArray() method of ArrayList you can get 0bject[].
Cast that Object[] to String[]
Here the sample code:

ArrayList<String> arr_List=new ArrayList<String>();
Object[] str_Arr=arr_List.toArray();

Questions:
Answers:

This is enough:

int[] d = new int[list.size()];

for (int i = 0; i < list.size(); i++) {
    d[i] = list.get(i);
}