在泛型数组类型 [英] Array type in generics

查看:155
本文介绍了在泛型数组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建泛型类型的数组。我得到错误:

I am trying to create an array of generic type. I am getting error:

 Pair<String, String>[] pairs;   // no error here

 pairs = new Pair<String, String>[10];   // compile error here

 void method (Pair<String, String>[] pairs)  // no error here.

我很困惑。任何线索为什么会这样。

I am confused. Any clues why this is happening.

推荐答案

这背后的原因是,你不能创建通用或参数化类型的数组,只有的 reifiable 的类型(即类型从而可以在运行时推断)。

The reason behind this is that you can't create arrays of generic or parameterized types, only reifiable types (i.e. types which can be deduced at runtime).

有可能的,虽然来的声明的如数组类型变量或方法参数。这是一个有点不合逻辑,但这是Java的是如何了。

It is possible though to declare such array types as variables or method parameters. This is a bit illogical, but that's how Java is now.

Java泛型和集合与此交易及相关问题进行了广泛的第6章。

Java Generics and Collections deals with this and related issues extensively in chapter 6.

这篇关于在泛型数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆