[SOLVED] How to convert the Object[] to String[] in Java?


I have a question about Java. I have an Object[] (Java default, not the user-defined) and I want to convert it to a String[]. Can anyone help me? thank you.


this is conversion

for(int i = 0 ; i < objectArr.length ; i ++){  
   try {
      strArr[i] = objectArr[i].toString();
   } catch (NullPointerException ex) {
       // do some default initialization

This is casting

String [] strArr = (String[]) objectArr;  //this will give you class cast exception


Tweak 1

 String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);


 Arrays.asList(Object_Array).toArray(new String[Object_Array.length]);

Note:That only works if the objects are all Strings; his current code works even if they are not

forTweak1 :only on Java 1.6 and above

Answered By – jmj

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *