【Java】数组强转问题
问题产生
问题代码:
List<String> strs = new LinkedList<String>();
// 中间有添加元素的操作,这里省略... // 这里toArray方法不会返回String类型的数组,但是写的时候不知道
Object[] objs = strs.toArray(); // 然后参数需要一个String数组类型
void someMethod(xx param1, String[] fieldNames, xxx param2, ...) // 入参就给的强转了
someMethod(p1, (String[]) objs, p2);
运行就会报错,报错信息:
Exception in thread “main” java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
细节分析参考:
https://blog.csdn.net/xxxxxxxxxxxyn/article/details/97694144
解决方案:
第一种,调用 toArray的重载:
List<String> fieldNames = new LinkedList<>();
// 省略元素装填过程
String[] strings = new String[fieldNames.size()];
String[] strings1 = fieldNames.toArray(strings);
第二种,差不多意思,就自己来操作元素装填
List<String> fieldNames = new LinkedList<>();
colList.forEach(item -> {
fieldNames.add(item.getFieldName());
});
Object[] objects = fieldNames.toArray();
String[] strings = new String[objects.length];
for (int i = 0; i < objects.length; i++) {
strings[i] = objects[i].toString();
}
调用toArray的重载时,注意源码的操作:
/**
* Returns an array containing all of the elements in this list in proper
* sequence (from first to last element); the runtime type of the returned
* array is that of the specified array. If the list fits in the
* specified array, it is returned therein. Otherwise, a new array is
* allocated with the runtime type of the specified array and the size of
* this list.
*
* <p>If the list fits in the specified array with room to spare
* (i.e., the array has more elements than the list), the element in
* the array immediately following the end of the collection is set to
* <tt>null</tt>. (This is useful in determining the length of the
* list <i>only</i> if the caller knows that the list does not contain
* any null elements.)
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
【Java】数组强转问题的更多相关文章
- 关于Object[]数组强转成Integer[]类型的数组.
为什么不能由Object[]数组强转成Integer[]数组. Object[] ins= { new Integer(0), new Integer(1), new Integer(2), new ...
- 关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
一.当把Object数组,强转的具体的Integer数组时,会报错. 代码如下: //数组强转报错演示 Object[] numbers = {1,2,3}; Integer[] ints = (In ...
- Java 数组最佳指南,快收藏让它吃灰
两年前,我甚至写过一篇文章,吐槽数组在 Java 中挺鸡肋的,因为有 List 谁用数组啊,现在想想那时候的自己好幼稚,好可笑.因为我只看到了表面现象,实际上呢,List 的内部仍然是通过数组实现的, ...
- Java核心基础第4篇-Java数组的常规操作
Java数组 一.数组简介 数组是多个相同类型数据的组合,实现对这些数据的统一管理 数组属引用类型,数组型数据是对象(Object) 数组中的元素可以是任何数据类型,包括基本类型和引用类型 数组类型是 ...
- Java 数组
数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如numbers[100 ...
- 第5章 Java数组
1.什么是数组 数组可以想象成一个巨大的盒子,这个盒子里面存放的是同一个数据类型的数据 例如:int[] scores = {78,68,94,93}; 2.如何使用Java中的数组 2.1申明数组 ...
- Java 数组基础
数组 数组(Array):相同类型数据的集合. 定义数组 方式1(推荐,更能表明数组类型) type[] 变量名 = new type[数组中元素的个数]; 比如: int[] a = new int ...
- Java数组及其内存分配
几乎所有的程序设计语言都支持数组.Java也不例外.当我们需要多个类型相同的变量的时候,就考虑定义一个数组.在Java中,数组变量是引用类型的变量,同时因为Java是典型的静态语言,因此它的数组也是静 ...
- [转载]Java数组扩容算法及Java对它的应用
原文链接:http://www.cnblogs.com/gw811/archive/2012/10/07/2714252.html Java数组扩容的原理 1)Java数组对象的大小是固定不变的,数组 ...
- Java数组技巧攻略
Java数组技巧攻略 0. 声明一个数组(Declare an array) String[] aArray = new String[5]; String[] bArray = {" ...
随机推荐
- Qt-不规则窗口
1 简介 参考视频:https://www.bilibili.com/video/BV1XW411x7NU?p=44 实现效果如下: 图片背景透明,可通过鼠标左键拖动图片(移动时以左上角为坐标),右 ...
- C# || 批量翻译工具 || 百度翻译api || 读取.cs文件内容 || 正则表达式筛选文件
背景: 我们项目一开始的所有提示都是中文,后来要做国际化.发现项目中的带双引号的中文居然有 2.3 w 多条!!!简直让人欲哭无泪... 如果使用人工改的话,首先不说正确率了.光是效率都是难难难.所以 ...
- THUWC 2024 游记
其实这个游记也没啥好写的-- day 0 上午做动车两个小时到重庆,路上玩了 1.5h 的星露谷. 下午去巴蜀中学报道试机,系统是 Ubuntu Jammy,大大的好评,只是桌面是 Xubuntu/X ...
- C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 文件夹体积很大
现象:大量调用.p12证书时,C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 文件夹变得越来越大. 调用代码: X509Certificate2 x50 ...
- 【干货分享】.NET人脸识别解决方案
前言 前段时间有同学在DotNetGuide技术社区交流群提问:.NET做人脸识别功能有什么好的解决方案推荐的吗?今天大姚给大家推荐2款.NET开源.免费.跨平台.使用简单的人脸识别库,希望可以帮助到 ...
- Flash驱动控制--芯片擦除(SPI协议)
摘要: 本篇博客具体包括SPI协议的基本原理.模式选择以及时序逻辑要求,采用FPGA(EPCE4),通过SPI通信协议,对flash(W25Q16BV)存储的固化程序进行芯片擦除操作. 关键词:SPI ...
- Python优雅遍历字典删除元素的方法
在Python中,直接遍历字典并在遍历过程中删除元素可能会导致运行时错误,因为字典在迭代时并不支持修改其大小.但是,我们可以通过一些方法间接地达到这个目的. 1.方法一:字典推导式创建新字典(推荐) ...
- python实现推送消息到微信公众号
使用到库: Requests 实现方式: 微信已开放了对应的接口,直接通过python的requests库,发起请求,实现推送消息到公众号 微信公众号准备: 1.没有注册微信公众号,可以使用微信提供的 ...
- 最新扣子(Coze)实战案例:扣子卡片的制作及使用,完全免费教程
♂️ 大家好,我是斜杠君,手把手教你搭建扣子AI应用. ☘️ 本文是<AI应用开发系列教程之扣子(Coze)实战教程>,完全免费学习. 关注斜杠君,可获取完整版教程. 如果想学习AI应用 ...
- 国产RK3568J基于FSPI的ARM+FPGA通信方案分享
近年来,随着中国新基建.中国制造 2025 规划的持续推进,单 ARM 处理器越来越难胜任工业现场的功能要求,特别是如今能源电力.工业控制.智慧医疗等行业,往往更需要 ARM + FPGA 架构的处理 ...