how to convert Object array to String array in java


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

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements