【问题标题】:adding objects to linkedlist/ arraylist in java在java中将对象添加到linkedlist/arraylist
【发布时间】:2011-07-03 08:26:44
【问题描述】:

我创建了一个存储类并将其用作我的数组列表/链接列表的数据类型。

private LinkedList bid_history;

我已在我的结构中将其初始化为

bid_history=new LinkedList <Bid_History> ();

我使用 add 将新项目添加到列表中,如下所示

bid_history.add(new Bid_History(bid_count,unit_price,bid_success));

经过“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();
        }
}

打印代码如下

int count=0,size;
size=bid_history.size();
while(count<size)
System.out.println(((Bid_History)bid_history.get(count++)).getBid_amount());

【问题讨论】:

  • 您显示的代码似乎没有任何问题。显示您的(编辑的)循环代码。我们需要更多信息来帮助您。另外,你怎么知道它们都是一样的?也向我们展示该代码
  • 您是否在连续迭代中更改bid_count, unit_price, bid_success 的值?
  • bid_success 被另一个函数修改。
  • 它不会解决您遇到的问题,但您应该将您的列表声明为 List bid_history 以使其成为参数化列表。就像现在一样,它是一个原始的 LinkedList(即可以包含任何内容的列表)
  • 另外,请遵守 Java 命名约定(无下划线),仅在需要时声明变量,并使用 Thread.sleep 休眠而不是繁忙循环。

标签: java arraylist linked-list


【解决方案1】:

另一种可能性是 BidHistory(count, price,success) 没有做正确的工作并且没有设置正确的字段。我不想猜测,但可能是您在类中使用静态计数/价格/成功字段,而不是 BidHistory 中的字段。

构造函数应该看起来像(“this.”很重要):

public BidHistory(int count, float price, boolean success) {
    this.count = count;
    this.price = price;
    this.success = success;
}

【讨论】:

  • 你是对的 jarek。我使用静态变量并使用 eclipse 生成 getter 和 setter,实际上并没有仔细查看。
【解决方案2】:

我能想到的对您的问题的唯一解释是bid_countunit_pricebid_success 的值在每次迭代中都不会改变。

【讨论】:

    【解决方案3】:

    我建议进行以下更改以使代码更容易:

    private final List<BidHistory> bidHistory = Lists.newLinkedList();
    

    final 确保该列表不能被另一个列表替换。泛型可防止您意外将不兼容的对象添加到列表中。它还使循环列表更容易。 Google Guava 的 Lists 类使代码保持简短,因为您不必两次提及泛型数据类型。

    BidHistory 类中的所有字段也应设为final,因此您以后无法更改它们。既然这是关于历史的,你以后无论如何都不能改变事实。

    private void printHistoryForDebugging() {
      for (BidHistory bid : bidHistory) {
        System.out.println(bid.getBidAmount() + " (hashCode " + System.defaultHashCode(bid) + ")");
      }
    }
    

    我选择打印每个出价的defaultHashCode 以检查对象是否相同。对于不同的对象,defaultHashCode 也很可能不同。

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2013-12-13
      • 1970-01-01
      • 2015-04-09
      • 2013-11-06
      • 1970-01-01
      • 2023-04-06
      • 2020-05-27
      • 2013-10-30
      相关资源
      最近更新 更多