![](/img/trans.png)
[英]java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
[英]java.lang.ClassCastException: [Ljava.lang.Object; incompatible with [Ljava.lang.String;
我正在尝试从linkedhashset检索随机元素。 以下是我的代码,但每次都会给我异常。
private static void generateRandomUserId(Set<String> userIdsSet) {
Random rand = new Random(System.currentTimeMillis());
String[] setArray = (String[]) userIdsSet.toArray();
for (int i = 0; i < 10; ++i) {
System.out.println(setArray[rand.nextInt(userIdsSet.size())]);
}
}
以下是我得到的例外-
java.lang.ClassCastException: [Ljava.lang.Object; incompatible with [Ljava.lang.String;
谁可以帮我这个事? 有没有更好的方法可以做到这一点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.