请问这里为什么不支持类型转换?

2017-02-12 19:23:53 +08:00
 Adia
	public static void main(String[]args){
		List<String> x = new ArrayList<String>();
		x.add("sherlock");
		//Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
		String[] y = (String[]) x.toArray();
		//为什么这样就 ok 了
		String[] y1 = x.toArray(new String[0]);
	}

有点难以理解。麻烦各位大大不吝赐教,先谢过为敬。

2437 次点击
所在节点    Java
9 条回复
hexzhou
2017-02-12 19:59:32 +08:00
`bArr = new B[]; A[] aArr = (A[]) bArr;`

"works" at runtime if and only if B is a subtype of A (or A itself). Whether B actually only contains As is irrelevant and the compile-time type of bArr is not used either (what matters is the runtime type):
MartinDai
2017-02-12 20:09:12 +08:00
在 SF 问的也是楼主吧
SoloCompany
2017-02-12 20:21:51 +08:00
你这个问题应该精简为

Object[] x = new Object[1];
String[] y = (String[]) x;

为什么编译不通过(或者多加一些隐式转换后,是运行时 ClassCastException)

但这不是明显的吗,这两个类型就是不相容的啊

假如 Java 规范允许这种强制类型转换(假设每个 item 都能转换)

那么你考虑以下以下语句执行是什么后果

x[0] = new Object();

ClassCastException 显然不合适,然后为了这个异常再加一种运行时异常?
ReisenZ
2017-02-12 20:24:06 +08:00
看源码....里面有这么一句...
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
无参方法创建的是一个 Object 数组,强转当然会失败
Adia
2017-02-12 20:26:02 +08:00
@MartinDai 不,因为我也不懂。那时还没人答出来,好奇心驱使了我- -
akking
2017-02-12 22:32:52 +08:00
Java Geneirc 的坑, 以及 Java 中 array 是 covariant 两个原因造成的。
sorra
2017-02-12 22:53:51 +08:00
array 是特殊的,不支持泛型, Object[]和 String[]不兼容
要调用 toArray(new String[]{})得到 String[]
leafin
2017-02-13 11:09:00 +08:00
List.toArray()返回的是 Object[], List.toArray(String[])返回的是 String[], Object[]不能强转为 String[],很好懂
leafin
2017-02-13 11:20:56 +08:00
如果你的疑问是,明明声明的是 List<String>,为什么 toArray()里面不能得到<String>而将结果输出为 String[],

因为 List<String>跟 List<Object>是同一个类型,你没法检测一个 List 被限定为只能存储什么元素,这个是运行时才知道的,但是这段代码你现在就得写完。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/339955

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX