【发布时间】:2023-04-03 07:13:06
【问题描述】:
我有一个名为 MatchingLine 的类
public class MatchingLine implements Comparable
{
private String matchingLine;
private int numberOfMatches;
// constructor...
// getters and setters...
// interface method implementation...
}
我在 ArrayList 中使用这个类如下 -
ArrayList<MatchingLine> matchingLines = new ArrayList<MatchingLine>();
但是,Netbeans IDE 在此声明旁边放了一条注释并说,
redundant type arguments in new expression (use diamond operator instead)
它建议我使用 -
ArrayList<MatchingLine> matchingLines = new ArrayList<>();
我一直认为以前的风格是惯例?后一种风格是惯例吗?
【问题讨论】:
-
顺便说一句,鼓励开发人员尽可能使用抽象类型的变量。所以
Collection<?> var或List<?> var是首选。 -
@Alexey:在某些情况下首选。但是,根据您的操作, List> 可能无法使用。根本不可能向 List> 添加新对象(除了 null)。
标签: java list coding-style netbeans-7 java-7