Java - Deprecated method - What to do?

后端 未结 3 1691
悲哀的现实
悲哀的现实 2021-01-06 09:25

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

Link: http://www.youtube.com/watch?v=9z_8yEv7nIc&featur

相关标签:
3条回答
  • 2021-01-06 09:51

    I looked at the tutorial in question.

    For your question about API, you would need to do the following:

    rightList.setListData(leftList.getSelectedValuesList().toArray());
    

    PS: some tips about style. In Java, variables usually begin with a lowercase alphabet and class names begin with an uppercase alphabet. In your code above, it looked to me that you were trying to call a static method on a class, so you might want to change the names to lowercase.

    0 讨论(0)
  • 2021-01-06 09:57

    You'd need to update the setListData method to take the new parameter type (and any other code that was expecting an Object[], including methods, possible things that iterate over the array, etc.) Just because something is deprecated doesn't mean it's removed, though.

    What to do depends on your immediate goal: is it to learn the material, or to learn the material and update all the source to compile without warnings.

    0 讨论(0)
  • 2021-01-06 10:02

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

    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.

    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)

    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"; )

    0 讨论(0)
提交回复
热议问题