什么是<>对于java泛型是什么意思? [英] What does <> mean for java generics?

查看:127
本文介绍了什么是<>对于java泛型是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码:

  class MyClass< RCM> 
私人列表< RCM> allPreExistingConfigsForCodes(){
if(this.allCodesForThisType.size()== 0)
返回新的ArrayList< RCM>(0);

IntelliJ告诉我应该替换 new ArrayList< RCM> with new ArrayList<> 这意味着什么?

解决方案

Java教程泛型课程



lockquote

在Java SE 7和更高版本中,您可以将
所需的类型实参替换为一个类型为$ b的空集合的泛型类的构造函数$ b参数(<> ),只要编译器可以从上下文中确定或推断
类型的参数。这对尖括号<>
,非正式地称为钻石。例如,您可以使用以下语句创建一个实例
Box< Integer>

盒及LT;整数> integerBox = new Box<>();



I have a bit of code:

class MyClass<RCM> 
  private List<RCM> allPreExistingConfigsForCodes() {
    if(this.allCodesForThisType.size() == 0)
       return new ArrayList<RCM>(0);

IntelliJ is telling me I should replace new ArrayList<RCM> with new ArrayList<> what would that mean?

解决方案

From the Java Tutorials generics lesson:

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box<Integer> with the following statement:

Box<Integer> integerBox = new Box<>();

这篇关于什么是&lt;&gt;对于java泛型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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