Gan*_*Gan 1 java linked-list arraylist
我创建了一个存储类,并用作我的arraylist/Linked列表的数据类型.
private LinkedList bid_history;
Run Code Online (Sandbox Code Playgroud)
我在我的结构中初始化了这个
bid_history=new LinkedList <Bid_History> ();
Run Code Online (Sandbox Code Playgroud)
我使用add添加新项目到列表中,如下图所示
bid_history.add(new Bid_History(bid_count,unit_price,bid_success));
Run Code Online (Sandbox Code Playgroud)
在'n'次迭代后,我检查了列表的内容,发现列表中有'n'个元素,但它们是相同的.即我添加的最后一个元素占据了整个列表.就好像我在列表中添加了一个引用变量?
知道我可能犯了哪个错误吗?我也使用了一个arraylist,同样的问题.我猜我访问说明符做错了!但我没有想法.....
----添加-------我使用递归函数
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();
}
}
Run Code Online (Sandbox Code Playgroud)
打印代码如下
int count=0,size;
size=bid_history.size();
while(count<size)
System.out.println(((Bid_History)bid_history.get(count++)).getBid_amount());
Run Code Online (Sandbox Code Playgroud)
另一种可能性是BidHistory(计数,价格,成功)没有做正确的工作而没有设置正确的字段.我不想猜,但可能不是在BidHistory中有字段而是在类中使用静态计数/价格/成功字段.
构造函数应该看起来像("this."很重要):
public BidHistory(int count, float price, boolean success) {
this.count = count;
this.price = price;
this.success = success;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1664 次 |
| 最近记录: |