how to convert Object array to String array in java

Java 8Object Oriented ProgrammingProgramming

As list.toArray() returns an Object[], it can be converted to String array by passing the String[] as parameter. See the example below.

import java.util.ArrayList;
import java.util.List;
public class Tester {
   public static void main(String[] args) {
      List<String> data = new ArrayList<String>();
      data.add("A");
      data.add("B");
      data.add("C");
      //Object[] objects = data.toArray();
      String[] strObjects = data.toArray(new String[0]);
      for(String obj: strObjects) {
         System.out.println(obj);
      }
   }
}

Output

A
B
C
raja
Published on 06-Feb-2018 15:23:20
Advertisements