我正在做我的第三年編程項目及其作品對象跟蹤器。 :/我已裝箱Stock_API和Portfolio_API接口(以及它們的實現方式),並且實例化時的GUI類有兩個參數爲這樣:Java - 將一個對象添加到數組列表,然後向數組列表添加另一個對象,導致第一個元素被覆蓋
public GUI(Portfolio_API p, Stock s){
tempPort = p;
tempStock = s;
}
我使用此構造爲獲得這些接口的實現進入的方式GUI而不將實現暴露給GUI(這是該項目的主要目標之一)。投資組合對象有一個名稱(字符串)和一個ArrayList。股票對象具有股票代碼(字符串),股票名稱(字符串),股票價值(浮動),股數(int)和持有價值(浮動)。
在GUI中,我有一個portCollection數組列表,用於存放portfolio_API類型的對象,這樣系統就可以跟蹤多個組合。另外如上面代碼塊中提到的,它有一個tempStock和tempPort對象。
對不起,給你這麼多關於該計劃的細節,但我認爲這是最好的,所以我可以得到上下文。無論如何,這個問題在眼前。我有一種方法,使用GUI獲取股票代碼,股票名稱和股票數量,並將股票添加到當前投資組合(每個投資組合都有其自己的選項卡)。該方法如下所示:
public void addStock() {
int num_shares = 0;
float dailyChange = 0.0f;
float stockValue = 0.0f;
boolean succeed = true;
// GUI gets information of stock from user
String ticker = JOptionPane.showInputDialog(frame,
"Enter the ticker symbol:");
String stockName = JOptionPane.showInputDialog(frame,
"Enter the Stock name:");
try {
num_shares = Integer.parseInt(JOptionPane.showInputDialog(frame,
"Enter the number of shares:"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame,
"Number of shares was not an integer. Try again");
succeed = false;
}
// If parsing was successful...
if (succeed) {
tempStock.setTicker(ticker);
tempStock.setNumberOfShares(num_shares);
tempStock.setStockName(stockName);
// Fetches newest value using the current ticker symbol
boolean succeedUpdate = tempStock.updateShareValue();
if (succeedUpdate) {
tempStock.calculateValueOfHolding();
// Adds to the current portfolio...
String tabName = tabbedPane.getTitleAt(tabbedPane
.getSelectedIndex());
System.out.println(tabName);
findPortfolio(tabName).addStock(tempStock);
findPortfolio(tabName).sort();
// ...Then adds it to the table
JPanel j = (JPanel) tabbedPane.getSelectedComponent()
.getComponentAt(0, 0);
JViewport v = ((JScrollPane) j.getComponent(0)).getViewport();
JTable table = (JTable) v.getComponent(0);
float currentTotal = findPortfolio(tabName).getTotal();
// Updates the total label
((JLabel) j.getComponent(1)).setText("Total: " + currentTotal);
Object[] newStock = { tempStock.getTicker(),
tempStock.getStockName(),
tempStock.getNumberOfShares(),
tempStock.getShareValue(),
tempStock.getValueOfHolding() };
((DefaultTableModel) table.getModel()).addRow(newStock);
}
}
}
當我添加多個股票時,新股票將替換舊股票並有效覆蓋它。我認爲它可以重複使用tempStock。不知道爲什麼儘管如果我將變量添加到一個數組列表它成爲該數組列表的一部分,並且不需要與tempStock變量關聯?與該提到的ArrayList使用
方法:
private Portfolio_API findPortfolio(String name) {
Portfolio_API p = null;
for (int i = 0; i < portCollection.size(); i++) {
if (portCollection.get(i).getName() == name) {
p = portCollection.get(i);
}
}
這兩個是在投資組合類:
@Override
public boolean addStock(Stock_API s) {
if (!doesExist(s)) {
portfolio.add(s);
return true;
} else {
return false;
}
}
@Override
public boolean doesExist(Stock_API s) {
boolean found = false;
for (int i = 0; i < portfolio.size(); i++) {
if (portfolio.get(i).getTicker() == s.getTicker()) {
found = true;
}
}
return found;
}
我只有來到這裏尋求幫助,因爲我已經撞南牆我真的需要幫助。如果有人可以給我任何建議,我會永遠在你的債務。
謝謝, 克里斯
你還沒有顯示任何使用'ArrayList'的代碼,這使得它很難把它釘住... – 2010-12-11 19:53:44
對不起,將編輯帖子並添加一些使用數組列表的方法。 – ChrisMacDee 2010-12-11 19:58:26
當您將一個對象「添加」到列表中時,您將添加對該對象的引用,而不是該對象的副本。如果稍後更改該對象,則該對象的所有引用都將看到該更改。 – 2010-12-11 20:15:29