在 Java 中将对象转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14245533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:35:58  来源:igfitidea点击:

Casting Object to Array in Java

javaarrayscasting

提问by Rafay

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

可能的重复:
如何在 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?

有没有更好更干净的方法来做到这一点?

回答by Juvanis

Why not directly casting?

为什么不直接投?

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