Converting ArrayList<String> to String[] in java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 06-Feb-2018 12:52:19
Advertisements