我正在學習一系列Java教程。我有一個關於教程的問題72.Java - 已棄用的方法 - 該怎麼辦?
鏈接:http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu
在視頻的7:02,這句話被寫入。但是,此方法在Java 1.7中已被棄用。
RightList.setListData(LeftList.getSelectedValues());
Eclipse中返回以下錯誤:
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[]) in the type JList is not applicable for the arguments (List)'
。
替換上述語句的正確方法是什麼?
另外,我想借此機會問另一個無關的問題。它是更好地初始化變量的方法外,像這樣:
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"};
相比(如視頻所示):聲明變量,如上面的類外,但裏面的方法分配值給他們?
要麼性能更好?
你在問初始化*變量,而不是*聲明*它們。此外,這不是「課外」,而是一種方法。初始化*變量的位置取決於它們的使用方式,通常是一種偏好問題。性能差異通常可以忽略不計,除非您創建*巨大數量的數字並在創建時初始化變量,而不是根據需要進行初始化。 –
好的,謝謝你的回覆。我傾向於交替使用這樣的術語,對於任何混淆抱歉。我應該使用正確的術語,謝謝提醒。 =) – Deley
'LeftList'是什麼類型的變量?它是JList嗎?同時顯示實際的錯誤信息,而不是你對它的解釋。 –