Java 中的菱形运算符是什么? [英] What is the diamond operator in Java?

查看:42
本文介绍了Java 中的菱形运算符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为 Patient_class 的数组列表,数组列表类型已用黄色下划线标出,IDE 已提到新表达式中的冗余类型参数(改用菱形运算符)".

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 use the diamond operator instead? Is it a must? Will I get any data loss or any other problem when storing records to the 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 ArrayList<patient_class>patientlist = new ArrayList<patient_class>();

老实说,我不知道钻石操作符到底是什么.

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

推荐答案

别担心.这不是邪恶.这是 Java 7 的特性.

Don't worry. It's not an evil. It's feature of Java 7.

菱形运算符的目的是简化泛型类的实例化.

The purpose of the diamond operator is to simplify instantiation of generic classes.

例如,代替

List<Map<Integer,Set<String>>> p = new ArrayList<Map<Integer,Set<String>>>();

使用菱形运算符我们只能写

with the diamond operator we can write only

List<Map<Integer,Set<String>>> 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.

这篇关于Java 中的菱形运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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