将HashSet转换为数组时抛出ArrayStoreException

编程入门 行业动态 更新时间:2024-10-18 10:37:33
本文介绍了将HashSet转换为数组时抛出ArrayStoreException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有如下代码:

int[] ho = new int[10]; ho[0]= 1; ho[2]= 1; ho[4]= 5; HashSet<Integer> hs = new HashSet(Arrays.asList(ho)); Integer[] a = hs.toArray(new Integer[hs.size()]);

这段代码对我来说很有意义,但是当我运行它时会抛出一个ArrayStoreException.这是为什么? HashSet是一组Integer s,输出数组也是如此.

This code makes perfect sense to me, but it throws an ArrayStoreException when I run it. Why is that? The HashSet is a set of Integers, and so is the output array.

推荐答案

ArrayStoreException:如果指定数组的运行时类型不是该集合中每个元素的运行时类型的超类型"

ArrayStoreException: "if the runtime type of the specified array is not a supertype of the runtime type of every element in this set"

ArrayStoreException表示试图将错误类型的对象存储到对象数组中.

ArrayStoreException is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.

尝试切换int[] to Integer[]

更多推荐

将HashSet转换为数组时抛出ArrayStoreException

本文发布于:2023-11-10 02:52:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   抛出   HashSet   ArrayStoreException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!