Java - 不推荐的方法 - 怎么办? [英] Java - Deprecated method - What to do?

查看:139
本文介绍了Java - 不推荐的方法 - 怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一系列Java教程以尝试学习它。我对教程72有疑问。

I am following a series of Java tutorials in an attempt to learn it. I have a question about tutorial 72.

链接: http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu

在7:02视频,这句话是写的。但是,此方法已在Java 1.7中弃用。

At 7:02 of the video, this statement is written. However, this method has been deprecated in Java 1.7.

RightList.setListData(LeftList.getSelectedValues());

Eclipse返回以下错误:

Eclipse returns the following error:

    Object[] javax.swing.JList.getSelectedValues()
    getSelectedValues
    @Deprecated
    public Object[] getSelectedValues()
    Deprecated. As of JDK 1.7, replaced by getSelectedValuesList()
    Returns an array of all the selected values, in increasing order based on their indices in the list.
    Returns:
    the selected values, or an empty array if nothing is selected
    See Also:
    isSelectedIndex(int), getModel(), addListSelectionListener(javax.swing.event.ListSelectionListener)

但这会返回一个错误,说'方法 setListData(Object [ ])JList类型不适用于参数(List)'

But this returns an error saying 'The method setListData(Object[]) in the type JList is not applicable for the arguments (List)'.

更换上述语句的正确方法是什么?

What is the correct way to replace the above statement?

另外,我想借此机会提出另一个不相关的问题。最好在方法之外初始化变量,如下所示:

Also, I want to take this opportunity to ask a another unrelated question. Is it better to initialize variables outside the method like so:

    private         JList       LeftList    =   new JList();
    private         JList       RightList   =   new JList();
    private         JButton     Move        =   new JButton("Move -->");

    private static  String[]    Items       =   {"Item 1", "Item 2","Item 3","Item 4","Item 5"};

与(如视频中所示)相比:在类之外声明变量,如上所述,但是赋值在方法里面给他们?

Compared to (As shown in the video): Declaring variables outside the class like above, but assigning values to them inside the method?

是否表现更好?

推荐答案

根据 JList Java7的javadoc我看到你确实没有选择 - 两个API( getSelectedValuesList setDataList )不相关。

According to JList javadoc for Java7 I see that indeed you have no option - the two APIs (getSelectedValuesList and setDataList) are unrelated.

要解决这个问题,一个简单的解决方案就是执行 LeftList。 getSelectedValuesList()。toArray() - 它将为您提供适合 setDataList 的数组。免责声明:我不知道这是否是Java推荐的正确用法,但它应该可行。

To solve it, a simple solution would be to perform LeftList.getSelectedValuesList().toArray() - it will provide you with an array suitable for setDataList. Disclaimer: I don't know if this is the "correct" usage recommended by Java, but it should work.

另外,请注意,弃用的API并不代表它不起作用 - 如果你觉得你现在不想花时间,你仍然可以使用旧的API(比如你正在做一个教程的情况,而不是一些正在制作的正在制作的产品)接下来的10年)

Also, note that a deprecated API does not mean it doesn't work - if you feel you don't want to invest time in it now, you can still use the old API (like in your situation where you are doing a tutorial and not some ongoing product that will be in production for the next 10 years)

至于第二个问题 - 这是一个品味问题,我更喜欢声明变量而不在类声明中初始化它们并使用值设置它们构造函数。习惯上给常量赋予初始值(例如 public static final String AAA =XYZ;

As for the 2nd question - it is a matter of taste, I prefer declaring the variables without initializing them in the class declaration and setting them with values in the constructor. It is customary to give initial values to constants (e.g. public static final String AAA = "XYZ"; )

这篇关于Java - 不推荐的方法 - 怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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