To replace an object array with a String array in Java

Asked 2 years ago, Updated 2 years ago, 52 views

Object Object_Array[]=new Object[100];

String String_Array[]=new String[Object_Array.length];

for (int i=0;i<String_Array.length;i++) String_Array[i]=Object_Array[i].toString();

Code to convert an object array to a String array. String_Array=(String[])Object_Array; I was thinking about another way, but a runtime error occurred

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

Isn't there a way to do it that way?

string java array

2022-09-22 16:19

1 Answers

There is a way to write System.arraycopy. String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);


2022-09-22 16:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.