简体   繁体   English

第i个元素的值更改影响ArrayList第0个元素的值

[英]value change in ith element influences value in the 0th element for ArrayList

I have the following code to set value by iteration. 我有以下代码通过迭代来设置值。

for (int i=0; i<list.size(); i++){
    QuestionaireResult item = list.get(i);

    List<QuestionaireResultDetail> rsDetails = item.getQuestionaireResultDetails();
    List<QuestionaireResultDetail> filledList = fillList2(rs.getQuestionaireResultDetails(), rsDetails);
    item.setQuestionaireResultDetails(insertCommentObj3(insertCommentObj3(filledList, "C"), "B"));

    QuestionaireResultDetail qr2 = new QuestionaireResultDetail();
    qr2.setAnswerDesc(item.getRemark());        
    item.getQuestionaireResultDetails().add(qr2);

    list.set(i, item);
 }

however, I find I make change in the ith element, the 0th element also changes its value as the ith element, but I don't think I change that. 但是,我发现我对ith元素进行了更改,第0元素也将其值更改为ith元素,但是我认为我没有更改。 Could someone give me advice about why that happen? 有人可以给我建议为什么会发生吗?

Thanks first. 首先谢谢。

Based on the description and the code given so far, I think there are only two possible (plausible) reasons: 根据到目前为止的描述和代码,我认为只有两个可能的(合理的)原因:

  • The i th item and the 0 th item are the same i个项目和第0个项目相同
  • There are static variables involved 涉及静态变量

The first one could happen, for example, when the list is filled like this: 例如,当列表如下填充时,可能会发生第一个:

List<Item> list = new ArrayList<Item>();
Item item = new Item();
for (int i=0; i<3; i++)
{
    item.setSomeProperty(i);
    list.add(item);
}

In this case, you should make sure that a new item is created for each entry of the list: 在这种情况下,您应确保为列表的每个条目创建一个项目:

List<Item> list = new ArrayList<Item>();
// Item item = new Item(); // Don't create a single instance here
for (int i=0; i<3; i++)
{
    Item item = new Item(); // Instead, always create a new instance
    item.setSomeProperty(i);
    list.add(item);
}

The second one could involve some static variable like in this example 第二个可能涉及一些静态变量,如本例所示

class Item
{
    private static int someProperty;

    public void setSomeProperty(int i)
    {
        someProperty = i;
    }
}

In this case, you just have to make sure that the field is not static . 在这种情况下,您只需要确保该字段不是static

If neither of both is the case, then the code that you provided is not sufficient for finding the reason for the odd behavior. 如果两种情况都不是,则您提供的代码不足以找到导致异常行为的原因。

Since your code is not complete, i am answering based on my Experience. 由于您的代码不完整,因此我根据我的经验进行回答。

Your List<QuestionaireResultDetail> is an ArrayList of Objects of Type QuestionaireResultDetail . 您的List<QuestionaireResultDetail>是一个类型为QuestionaireResultDetail的对象的ArrayList。

Now the ArrayList stored the Objects. 现在, ArrayList存储了对象。 So when you add an object to the ArrayList its not actualy creating a copy Object, its actually making a reference to the Original Object. 因此,当您将一个对象添加到ArrayList时,实际上并不是创建一个副本对象,而是实际上是对原始对象的引用。 Hence any change in One object will be reflected to all the copies of that Object. 因此,一个对象的任何更改都将反映到该对象的所有副本中。

For Eg: 例如:

Object A= 12;
Object B=A;
B=10;

will make the value of A as 10. Because A and B points to the same location. 将A的值设为10。因为A和B指向相同的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM