Java 重新编译 -Xlint:unchecked for details 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19846682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:40:09  来源:igfitidea点击:

Recompile with -Xlint:unchecked for details Warning?

java

提问by Hashan Wijetunga

I wrote this code where when the software starts it takes data from a database (MYSQL control center) and bring them to a table. But when I compile this code 2 errors occurs.

我写了这段代码,当软件启动时,它从数据库(MYSQL 控制中心)获取数据并将它们带到表中。但是当我编译这段代码时,会出现 2 个错误。

Note: C:\Users\Commander Shepard\Documents\NetBeansProjects\Furniture Management System\src\furnituremanagementsystem\employee.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

My Code:

我的代码:

public class employee extends javax.swing.JFrame {

    // Creates new form employee
    public employee() {
        initComponents();           

        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        datelabel.setText(formatter.format(now));

        try {
            Statement s = DB.getConnection().createStatement();
            DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
            dtm.setRowCount(0);
            ResultSet r = s.executeQuery("SELECT * from Employee");
            while (r.next()) {
                Vector v = new Vector();
                v.add(r.getString(1));
                v.add(r.getString(2));
                v.add(r.getString(3));
                v.add(r.getString(4));
                v.add(r.getString(5));
                v.add(r.getString(6));
                v.add(r.getString(7));
                v.add(r.getString(8));
                v.add(r.getString(9));
                v.add(r.getString(10));
                v.add(r.getString(11));
                v.add(r.getString(12));
                v.add(r.getString(13));
                v.add(r.getString(14));

                dtm.addRow(v);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    ...
}

The thing didn't actually affect the program, it's working properly, but I want to know if this is going to be a problem in the future and whether I should do something about this.

这件事实际上并没有影响程序,它运行正常,但我想知道这在未来是否会成为一个问题,以及我是否应该对此做些什么。

回答by rgettman

You used the raw form of the Vectorclass. It is a generic class. Since you're adding Strings, try

您使用了Vector的原始形式。它是一个泛型类。由于您要添加Strings,请尝试

Vector<String> v = new Vector<String>();

回答by Arash Saidi

That happens when you use generic references. You don't really need to take the warning into consideration, but if you want to see what it is you should compile again with the new argument:

当您使用通用引用时会发生这种情况。你真的不需要考虑警告,但如果你想看看它是什么,你应该用新参数再次编译:

javac programname.java argument

javac 程序名.java 参数

But using generics in Java has many problems as it was added quite late and with all the backward compatibility issues, yeah, we don't need to get into that.

但是在 Java 中使用泛型有很多问题,因为它添加得很晚,并且存在所有向后兼容性问题,是的,我们不需要深入研究。

回答by Martin Zeitler

Do as it says:

按照它说的做:

Note: Recompile with -Xlint:uncheckedfor details.

注意:重新编译以-Xlint:unchecked获取详细信息。

For example, with Gradle:

例如,使用 Gradle:

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {         
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

(Back then) with NetBeans, that would have been "Additional Compiler Options".

(当时)使用 NetBeans,那将是“附加编译器选项”。