报告局部变量声明和访问器,以记录增强型 'for' 语句(这些语句通常更紧凑)中可被替换为模式变量的组件。

示例:


record Record(Integer x, String y) {
}

public static void test(List<Record> records) {
    for (Record record : records) {
        System.out.println(record.y());
        Integer x = record.x;
        System.out.println(x);
    }
}

可以替换为:


record Record(Integer x, String y) {
}

public static void test(List<Record> records) {
    for (Record(Integer x, String y) : records) {
        System.out.println(y);
        System.out.println(x);
    }
}

2023.1 最新变化