Java:将对象转换为数组类型 [英] Java: Casting Object to Array type
问题描述
我正在使用一个返回Object类型的普通对象的Web服务。调试清楚地表明在这个对象中有某种数组,所以我想知道如何将这个对象转换为数组(或类似的)?
I am using a web service that returns a plain object of the type "Object". Debug shows clearly that there is some sort of Array in this object so I was wondering how I can cast this "Object" to an Array (or similar)?
我试过以下内容:
Collection<String> arr = (Collection<String>) values;
Vector<String> arr = (Vector<String>) values;
ArrayList<String> arr = (ArrayList<String>) values;
但没有任何效果。我总是得到一个InvocationTargetException。
But nothing worked. I always get an InvocationTargetException.
我做错了什么?
编辑 :
遗憾的是,我不得不删除显示Eclipse调试器输出的图像的链接,因为它已不再可用。请不要错过为什么在答案中提到的图像不再存在。
Sadly, I had to remove the link to the image that showed the output of Eclipse's debugger because it was no longer available. Please do not wonder why in the answers an image is mentioned that is not there anymore.
推荐答案
您的值
对象显然是一个 Object []
,其中包含一个包含值的 String []
。
Your values
object is obviously an Object[]
containing a String[]
containing the values.
String[] stringValues = (String[])values[0];
这篇关于Java:将对象转换为数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!