我創建了一個存儲類並用作我的數組列表/鏈接列表的數據類型。將對象添加到java中的linkedlist/arraylist
private LinkedList bid_history;
我在建設淺析初始化這是
bid_history=new LinkedList <Bid_History>();
我添加新的項目,使用添加如下圖所示
bid_history.add(new Bid_History(bid_count,unit_price,bid_success));
後「N」次迭代我檢查的內容列表發現列表中有'n'個元素,但它們是相同的。即我添加的最後一個元素佔據了整個列表。這就好像我添加了一個參考變量到列表中?
任何想法,我可能犯了一個錯誤?我也使用了數組列表,同樣的問題。我猜測我對訪問說明符做了一些錯誤!但我的想法.....
----添加------- 我使用遞歸函數
bid()
{
int bid,quantity;
bid_success=false;
bid_count++;
System.out.println("Starting to bid, Bid ID:"+bid_count);
quantity=(int)(rated_power*duration/60);
if(bid_history.isEmpty())
{
unit_price=10;
}
else
{
unit_price++;
}
bid=unit_price*quantity;
//Sending the calculated bid
send_res(unit_price,quantity,500);
long startTimeMs = System.currentTimeMillis();
System.out.println("Time:"+startTimeMs);
while(!(System.currentTimeMillis()>(startTimeMs+2000)));
System.out.println("Time at end:"+System.currentTimeMillis());
bid_history.add(new Bid_History(bid_count,unit_price,bid_success));
if(bid_success!=true)
{
bid();
}
}
印刷代碼如下
int count=0,size;
size=bid_history.size();
while(count<size)
System.out.println(((Bid_History)bid_history.get(count++)).getBid_amount());
您顯示的代碼似乎沒有任何問題。顯示你的(編輯的)循環代碼。我們需要更多信息來幫助你。另外,你怎麼知道他們都是一樣的?向我們顯示該代碼 – Bohemian
您是否在連續迭代中更改「bid_count,unit_price,bid_success」的值? –
'bid_success'被另一個函數修改。 – Gan