Converting ArrayList to String[] in java


Following program is converting an ArrayList<String> to String[];

Example

import java.util.ArrayList;
import java.util.List;

public class Tester {
   public static void main(String[] args) {
      List<String> names = new ArrayList<String>();
      names.add("A");
      names.add("B");
      names.add("C");
      String[] nameArray = names.toArray(new String[names.size()]);
      for(String name: nameArray) {
         System.out.println(name);
      }
   }
}

Output

A
B
C

Updated on: 24-Feb-2020

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements