如何对象数组转换为Java中字符串数组 [英] How to convert object array to string array in Java
本文介绍了如何对象数组转换为Java中字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的code为一个对象数组转换为一个String数组:
I use the following code to convert an Object array to a String array :
Object Object_Array[]=new Object[100];
// ... get values in the Object_Array
String String_Array[]=new String[Object_Array.length];
for (int i=0;i<String_Array.length;i++) String_Array[i]=Object_Array[i].toString();
但我不知道是否有另一种方式来做到这一点,是这样的:
But I wonder if there is another way to do this, something like :
String_Array=(String[])Object_Array;
但是,这将导致运行时错误:异常螺纹AWT-EventQueue的-0java.lang.ClassCastException:[Ljava.lang.Object;不能被转换为[Ljava.lang.String;
什么是做了正确的方法是什么?
What's the correct way to do it ?
推荐答案
另一种替代 System.arraycopy
:
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
这篇关于如何对象数组转换为Java中字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文