java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String,toArray向下转型失败_看风儿的博客-程序员宅基地

问题:

将集合通过toArray转为Object[]数组后,再通过向下转型失败

源代码:

public void test(){
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        String[] strings = (String[]) list.toArray();
        for (String string : strings) {
            System.out.println(string);
        }
 }

错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

	at Test14.test(Test14.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

解决代码:

    public void test(){
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        String[] temp = new String[3];
        String[] strings = list.toArray(temp);
        for (String string : strings) {
            System.out.println(string);
        }
    }

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wang037195/article/details/95768620

智能推荐

WinForm DataGridView增删改查-程序员宅基地

DataGridView连接数据库对表进行增删改查一、绑定数据源 //做一个变量控制页面刷新 public static int bs = 0; public Form1() { InitializeComponent(); } ...

Mongo 服务器上的 CPU 使用率很高,但 Mongo 似乎处于空闲状态_mongo cpu占用高_行走在云端z的博客-程序员宅基地

设置:我们在 4.2.13 版本中运行 MongoDB。副本集,主副本和两个副本。服务器有 4 个 CPU 和 16 GB 的 RAM(m5.xlarge 实例和 gp2 磁盘)并且只专用于 Mongo。Primary 主要用于写入,而我们的读取主要从副本执行。我们正在运行 Mongo,默认配置和 transactionLifetimeLimitSeconds 设置为 900。问题:在负载测试期间,我们经常遇到主节点卡住的情况。平均负载变为 ~9 )并且通过观察 mongotop 和 mongostat _mongo cpu占用高

springData操作mongodb查询使用_spring.data.mongodb.uri_啊明仔的博客-程序员宅基地

这里写自定义目录标题由于业务字段不确定性,把mysql数据库切换成了mongodb导入依赖和数据库配置事务和其他配置使用简单查询复杂查询 使用mongoTemplate聚合使用其他分页查询总结由于业务字段不确定性,把mysql数据库切换成了mongodb记录一下基础的mongodb数据库基本操作导入依赖和数据库配置依赖<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-da_spring.data.mongodb.uri

使用Sophus和pangolin实现轨迹误差_average translational error-程序员宅基地

绝对轨迹误差(Absolute Trahextiry,ATE)姿态的真实值与姿态的理想值之间的差,实际上是李代数的均方根误差(Root-Mean-Aquared,RMSE)绝对平移误差(Average Translational Error)平移部分真实值与姿态的理想值之间的差实现:..._average translational error

PPO(Proximal Policy Optimization)近端策略优化算法_ppo算法流程图_Wanderer001的博客-程序员宅基地

强化学习可以按照方法学习策略来划分成基于值和基于策略两种。而在深度强化学习领域将深度学习与基于值的Q-Learning算法相结合产生了DQN算法,通过经验回放池与目标网络成功的将深度学习算法引入了强化学习算法。其中最具代表性分别是Q-Learning与Policy Gradient算法,将Q-Learning算法与深度学习相结合产生了Deep Q Network,而后又出现了将两种方式的优势结合在一起的更为优秀Actor Critic,DPG, DDPG,A3C,TRPO,PPO等算法。而本文所采用的是目前_ppo算法流程图

系统详细设计(搜索得到的相关链接)_yangc的网站-程序员宅基地

1. https://wenku.baidu.com/view/24f67ca70d22590102020740be1e650e53eacf0a.html(7页)无示例,但详细列出了文档目录和简要内容要求2. https://blog.csdn.net/weixin_42216018/article/details/90279456在线考试系统详细设计(有示例,详细对每个功能模块的输入和输出进行了描述)3. https://www.cnblogs.com/hellowzl/p/115119._yangc的网站

随便推点

python float 精度_谈谈关于Python里面小数点精度控制的问题-程序员宅基地

基础浮点数是用机器上浮点数的本机双精度(64 bit)表示的。提供大约17位的精度和范围从-308到308的指数。和C语言里面的double类型相同。Python不支持32bit的单精度浮点数。如果程序需要精确控制区间和数字精度,可以考虑使用numpy扩展库。Python 3.X对于浮点数默认的是提供17位数字的精度。关于单精度和双精度的通俗解释:单精度型和双精度型,其类型说明符为float 单精..._python3的float数精度

华为机试—变形的杨辉三角形_杨辉三角形的变形,华为机试规律怎么找-程序员宅基地

变形的杨辉三角形 每个数是上行,左右3个数之和,不存在为0,求第n行第一个偶数出现的位置,没有输出-1。#includeusing namespace std;int main(int argc,char *argv[]){ int n,s,e; cin>>n; int a[101][201]={0}; //数组容量的_杨辉三角形的变形,华为机试规律怎么找

数列分段-程序员宅基地

题目描述对于给定的一个长度为NN的正整数数列AiAi​,现要将其分成连续的若干段,并且每段和不超过MM(可以等于MM),问最少能将其分成多少段使得满足要求。输入输出格式输入格式:第1行包含两个正整数N,MN,M,表示了数列AiAi​的长度与每段和的最大值,第22行包含NN个空格隔开的非负整数AiAi​,如题目所述。输出格式:一个正整数,输出最少划分的段数。输..._数列分段

SQL注入在先验证用户名的情况下绕过题目(2017-赛客夏令营-Web-Injection V2.0)-程序员宅基地

SQL注入在先验证用户名的情况下绕过题目前一篇文章就是介绍,在先验证用户名的情况下绕过。现在来简单的总结一下和做一道题。当我们登陆一个页面进行用户名和密码验证的时候,可能存在俩种方式。方法一:$username=$_POST['username'];$password=$_POST['password'];$sql='select * from user where username='$username' and password='$password'';#查询#登陆成功#第一种

pycharm 导包问题 No module named 'db2'_no module named db-程序员宅基地

db2.py 里a=10db1.py 里from .db2 import ab = aprint(b)若运行db1.py 会报错:Traceback (most recent call last): File &quot;C:/Users/hp/PycharmProjects/mY/y_pro/daobao/db1.py&quot;, line 1, in &amp;lt;module&amp;gt; ..._no module named db

将一个正整数划分成若干正整数的和,输出每一种划分方法-程序员宅基地

import java.util.ArrayList;//将一个正整数划分成若干正整数(不超过max)的和,输出每一种划分方法public class IntDivision { //递归对整数n进行划分,划分元素的最大值为max public static ArrayList> divideInt_r(int n, int max){ ArrayList> divisionList

推荐文章

热门文章

相关标签