为什么我的ArrayList中包含的最后一个项目的N份添加到列表中? [英] Why does my ArrayList contain N copies of the last item added to the list?

查看:201
本文介绍了为什么我的ArrayList中包含的最后一个项目的N份添加到列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了三种不同的对象一个ArrayList,但列表中包含我添加的最后一个对象的三个副本。

例如:

 的(富F:名单){
  的System.out.println(f.getValue());
}

预期:

  0
1
2

实际

  2
2
2

我已取得了哪些错误?

注意:这样的设计是一个典型的Q&安培; A对本网站中出现的许多类似问题


解决方案

这问题有两个典型的原因:


  • 在您的列表对象静态字段


  • 不小心添加的相同的对象名单


静态字段

如果你在你的列表对象的静态字段,在列表中的每个对象看起来似乎是一样的,因为他们持有相同的值。考虑下面的类:

 公共类Foo {
  私有静态int值;
  // ^^^^^^ ------------ - 这里的问题!  公共美孚(int值){
    THIS.VALUE =价值;
  }  公众诠释的getValue(){
    返回值;
  }
}

在这个例子中,存在着一个的所有实例之间共享,因为它只有一个 int值声明静态。 (请参见理解类成员教程。)

如果您添加多个反对使用下面的code,每个实例都将返回一个列表 4 从调用的getValue()

 的for(int i = 0;我4;;我++){
  list.add(新美孚(一));
}

解决方法很简单 - 除非你确实想要那个类的每个实例之间共享的价值观不使用字段静态的关键字在你的类。

添加相同的对象

如果您添加一个临时变量列表,则必须每次循环创建一个新的实例。请看下面的错误code片断:

 列表<富>名单=新的ArrayList<富>();
美孚TMP =新的Foo();的for(int i = 0;我3;;我++){
  tmp.setValue(ⅰ);
  list.add(TMP);
}

在这里, TMP 对象,构建循环外。其结果是,在同一个对象实例的被添加到列表中三次。实例将持有的值 2 ,因为这是的setValue最后调用()期间传递的值

要解决这个问题,只是移动对象的构造内循环:

 列表<富>名单=新的ArrayList<富>();的for(int i = 0;我3;;我++){
  美孚TMP =新的Foo(); //< - 新鲜的实例!
  tmp.setValue(ⅰ);
  list.add(TMP);
}

I'm adding three different objects to an ArrayList, but the list contains three copies of the last object I added.

For example:

for (Foo f : list) {
  System.out.println(f.getValue());
}    

Expected:

0
1
2

Actual:

2
2
2

What mistake have I made?

Note: this is designed to be a canonical Q&A for the numerous similar issues that arise on this site.

解决方案

This problem has two typical causes:

  • Static fields in your list object

  • Accidentally adding the same object to the list

Static Fields

If you have static fields in your list object, each object in your list will appear to be the same because they hold the same values. Consider the class below:

public class Foo {
  private static int value; 
  //      ^^^^^^------------ - Here's the problem!

  public Foo(int value) {
    this.value = value;
  }

  public int getValue() {
    return value;
  }
}

In that example, there is only one int value which is shared between all instances of Foo because it is declared static. (See "Understanding Class Members" tutorial.)

If you add multiple Foo objects to a list using the code below, each instance will return 4 from a call to getValue():

for (int i = 0; i < 4; i++) {      
  list.add(new Foo(i));
}

The solution is simple - don't use the static keywords for fields in your class unless you actually want the values shared between every instance of that class.

Adding the Same Object

If you add a temporary variable to a list, you must create a new instance each time you loop. Consider the following erroneous code snippet:

List<Foo> list = new ArrayList<Foo>();    
Foo tmp = new Foo();

for (int i = 0; i < 3; i++) {
  tmp.setValue(i);
  list.add(tmp);
}

Here, the tmp object was constructed outside the loop. As a result, the same object instance is being added to the list three times. The instance will hold the value 2, because that was the value passed during the last call to setValue().

To fix this, just move the object construction inside the loop:

List<Foo> list = new ArrayList<Foo>();        

for (int i = 0; i < 3; i++) {
  Foo tmp = new Foo(); // <-- fresh instance!
  tmp.setValue(i);
  list.add(tmp);
}

这篇关于为什么我的ArrayList中包含的最后一个项目的N份添加到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆