java: (String[])List.toArray() 给出 ClassCastException

发布于 2024-11-02 00:12:18 字数 303 浏览 8 评论 0原文

下面的代码(在android中运行)总是在第三行给我一个ClassCastException:

final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();

当v2是Object[0]并且其中有字符串时也会发生这种情况。 知道为什么吗?

The following code (run in android) always gives me a ClassCastException in the 3rd line:

final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();

It happens also when v2 is Object[0] and also when there are Strings in it.
Any Idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一瞬间的火花 2024-11-09 00:12:18

这是因为当您使用

 toArray() 

它时,它返回一个 Object[],它不能转换为 String[] (即使内容是字符串)这是因为 toArray 方法只获取 a

List 

而不是

List<String>

因为泛型只是源代码事物,并且在运行时不可用,因此它无法确定要创建什么类型的数组。

use

toArray(new String[v2.size()]);

分配正确类型的数组(String[] 和正确的大小)

This is because when you use

 toArray() 

it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray method only gets a

List 

and not

List<String>

as generics are a source code only thing, and not available at runtime and so it can't determine what type of array to create.

use

toArray(new String[v2.size()]);

which allocates the right kind of array (String[] and of the right size)

装迷糊 2024-11-09 00:12:18

您使用了错误的 toArray()

请记住,Java 的泛型主要是语法糖。 ArrayList 实际上并不知道它的所有元素都是字符串。

要解决您的问题,请调用 toArray(T[])。在您的情况下,

String[] v3 = v2.toArray(new String[v2.size()]);

请注意,泛型形式 toArray(T[]) 返回 T[],因此不需要显式转换结果。

You are using the wrong toArray()

Remember that Java's generics are mostly syntactic sugar. An ArrayList doesn't actually know that all its elements are Strings.

To fix your problem, call toArray(T[]). In your case,

String[] v3 = v2.toArray(new String[v2.size()]);

Note that the genericized form toArray(T[]) returns T[], so the result does not need to be explicitly cast.

溇涏 2024-11-09 00:12:18
String[] v3 = v2.toArray(new String[0]); 

也能做到这一点,
请注意,一旦为该方法提供了正确的 ArrayType,您甚至不需要再进行强制转换。

String[] v3 = v2.toArray(new String[0]); 

also does the trick,
note that you don't even need to cast anymore once the right ArrayType is given to the method.

海风掠过北极光 2024-11-09 00:12:18

使用 JDK 11 Stream API,您可以这样解决更一般的问题:

Object[] v1 = new String[] {"a", "b", "c"}; // or array of another type
String[] v2 = Arrays.stream(v1)
    .<String>map((Object v) -> v.toString()).toArray(String[]::new);

Using toArray from the JDK 11 Stream API, you can solve the more general problem this way:

Object[] v1 = new String[] {"a", "b", "c"}; // or array of another type
String[] v2 = Arrays.stream(v1)
    .<String>map((Object v) -> v.toString()).toArray(String[]::new);
莫多说 2024-11-09 00:12:18
String[] str = new String[list.size()];
str = (String[]) list.toArray(str);

像这样使用。

String[] str = new String[list.size()];
str = (String[]) list.toArray(str);

Use like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文