字符串字符串数组JAVA铸造名单 [英] JAVA casting list of strings to string array

查看:144
本文介绍了字符串字符串数组JAVA铸造名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作从字符串列表创建的字符串数组。到目前为止,我有以下的code:

I am working on creating an array of strings from a list of strings. So far, I have the following code:

ArrayList<String> layerChoices = new ArrayList<>();
for(IFeatureLayer layer : layerList){
   layerChoices.add(layer.getName());
}
String[] choices = (String[])layerChoices.toArray();

问题在于toArray()返回一个Object [],而不是一个字符串尝试转换为一个String []当它是生产类转换异常[]。有一个简单的方法来实现自己的目标比for循环中,我将通过Object []对象,投每个对象的字符串迭代,然后将每个字符串添加到一个String []等?好像很多工作,为一个简单的任务...

The issue being that toArray() returns an Object[] and not a String[] which is producing a class cast exception when trying to cast to a String[]. Is there a simple way to accomplish my goals other than a for loop in which I would iterate through the Object[] , cast each Object to a String, and then add each String to a String[]? Seems like a lot of work for a simple task...

推荐答案

使用它像这样 - 的String [] myArray的= myList.toArray(新的String [myList.size()]);

Use it like this - String [] myArray = myList.toArray(new String[myList.size()]);

这篇关于字符串字符串数组JAVA铸造名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆