java 钻石依赖,什么是Java中的钻石操作?

博客围绕Java中ArrayList类型使用时IDE提示用钻石操作符替代冗余类型参数展开。作者询问是否必须使用钻石操作符,使用时存储记录有无问题。随后介绍钻石操作符是Java 7特性,可简化泛型类实例化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have an arraylist with type patient_class and the arraylist type has been underlined in yellow and the IDE has mentioned "redundant type arguments in new expression (use diamond operator instead)".

My problem is should i used diamond operator instead ? is it a must? will i get any date lose or any other problem when storing records to the arraylist ?

Here is my arraylist :-

public class Register_newpatient extends javax.swing.JFrame {

public Register_newpatient() {

initComponents();

groupbutton();

}

public void groupbutton()

{

ButtonGroup bg1=new ButtonGroup();

bg1.add(rbopd);

bg1.add(rbinpatientcare);

bg1.add(rbboth);

}

all_error_handling checkerror=new all_error_handling();

JFrame frame=new JFrame();

static ArrayListpatientlist=new ArrayList();

To be honest i have no idea what is a diamond operator actually is.

解决方案

Don't worry.Its not an evil.Its feature of JAVA 7.

Purpose of the diamond operator is to simplify instantiation of generic classes. For example, instead of

List>> p = new ArrayList>>();

with the diamond operator we can write only

List>> p = new ArrayList<>();

If you want to know more about it and want to use it please have a quick look here and decide whether it's useful to you or not

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值