我遇到了我的代碼問題,我在那裏通過ArrayList
並獲取currentVehicle
並將其返回以顯示在GUI界面中。如何在完成ArrayList操作後停止返回null的方法?
我在達到ArrayList
的末尾時出現問題,它開始返回'null'。由於它返回'null',我無法返回到之前的車輛,因爲currentVehicle
值現在被分配爲'null'。
我一直在使用while
循環重啓循環,else, and else if
循環和使用lastIndexOf
和isEmpty
檢查ArrayList
,並試圖把它重新循環嘗試。
以下是ArrayList
的代碼,其中我正在搜索列表中的下一輛車,以及我相信該修補程序可能存在的位置。
Showroom.java
public Vehicle nextVehicle()
{
Vehicle nV = null;
try
{
if (currentVehicle < vehicleArray.size())
{
nV = vehicleArray.get(currentVehicle++);
}
}
catch(Exception e)
{
System.out.println("No more vehicles listed");
}
return nV;
}
GUIMain是其中輸出正在發生,我打印到一個JPanel文本區
GUIMain.java
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == nextButt)
{
log.setText(null);
log.append("" + s.nextVehicle());
}
}
log.setText(null);
清除JPanel
爲文本框要顯示的下一輛車和log.append("" + s.nextVehicle());
將當前車輛打印到JPanel
TextArea
。
任何幫助將不勝感激。如果需要其他代碼或需要SSCCE,我會繼續完成。
完美。感謝您的回答。 – 2014-11-01 05:25:27