Casting Object to Array in Java [duplicate]

混江龙づ霸主 提交于 2019-12-24 16:39:00

问题


Possible Duplicate:
How to convert object array to string array in Java

I am receiving an Object and casting it into a String array like this:

Object values[] = (Object[])request.getSession().getAttribute("userList");
String[] tmp = new String[values.length];
for(int i = 0; i < tmp.length; ++i) {
     tmp[i] = (String) values[i];
     out.println(tmp[i]);
}

Is there any better and cleaner way to do this?


回答1:


Why not directly casting?

String values[] = (String[])request.getSession().getAttribute("userList");


来源:https://stackoverflow.com/questions/14245533/casting-object-to-array-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!