0

I have two Classes:

Class1 which contains an ArrayList of type Class2

When I try to add a new object as follow:

Class2 object = new Class2();
Class1Object.getArrayList().add(object);

Then it appears that the object has been added when I iterate over getArrayList()

However I have another ArrayList of type class1 and when I iterate over this there object added does not appear?

I thought that since objects are by reference it should be added to the ArrayList of type class1. Can any one explain this please?

public class Subject implements Serializable {

private static final long serialVersionUID = 1L;
private String subjectName;
private int hours;
private int mins;
private ArrayList<Task> tasks;
private SimpleDateFormat date;

public Subject(String subjectName){
    this.subjectName = subjectName;
    hours = 0;
    mins = 0;
    tasks = new ArrayList<Task>();
    date = null;
}


public ArrayList<Task> getTasks() {
    return tasks;
}

}

public class Task implements Serializable {

    private static final long serialVersionUID = 2L;
    private String description;
    private boolean isCompleted;

    public Task(String description){
        this.description = description;
        isCompleted = false;
    }   

}

So then I have:

ArrayList<Subject> subjectsList = new ArrayList<Subject>();

And then I want to add a new task to a given subject so I do:

Task task = new Task(description);
ArrayList<Task> taskList = subject.getTasks();
taskList.add(task);

And when I iterate over subject.getTasks(); its there but when I iterate over subjectsList the new task is not there anymore.

Here is the first loop which shows the new task:

for (Task task : subject.getTasks()){
   System.out.println( task.toString() );
}

And the code for iterating over all objects from subjectsList

for (Subject s : subjectsList){
 for (Task t : s.getTasks()){
   System.out.println( t.toString() );
    }
}



Bundle bundle = getIntent().getExtras();
  if (bundle != null) {
    subject = (Subject) bundle.get("selected_subject");
    subjectsList = (ArrayList<Subject>) bundle.get("subjects_list");
   }
4

5 回答 5

4

所以你有两个列表,你将一个对象添加到这两个列表之一中,并期望在第二个列表中找到它。为什么它会在第二个列表中?它们是两个不同的列表。

Java 对象就像真实的对象。如果你把一条信息放在一个瓶子里,这条信息就不会出现在所有其他瓶子里。仅在您放置它的地方。

于 2013-02-01T23:28:45.107 回答
1

如果您创建 2 个变量指向同一个对象,则两个引用都将包含该值,例如:

Class2 object = new Class2();
Class1 class1 = new Class1();
Class1 class1second = class1;
class1.getArrayList().add(object);

class1 和 class1second 都包含对象

如果是

Class2 object = new Class2();
Class1 class1 = new Class1();
Class1 class1second = new Class1();
class1.getArrayList().add(object);

只有一个对象包含该值。

于 2013-02-01T23:28:12.863 回答
0

我猜这是一个范围问题。尝试使用This.Class1Object.getArrayList().add(object); ,因为也许您将对象添加到数组的本地化版本中。

于 2013-02-01T23:28:29.187 回答
0

我不明白这个问题。很难理解。

但是,如果您的意思是 Class1Object 的第二个实例具有相同的 ArrayList 或者不是某个您应该注意静态的实例。

如果 ArrayList 的变量是静态的,那么这些 Class 的所有实例都共享同一个 ArrayList 实例!

除非必要,否则避免使用静态。

如果这不能解决您的问题,请提供更多示例代码。

于 2013-02-01T23:28:42.123 回答
0

似乎您是在说您有两个列表。并且您期望添加到其中一个列表的对象自动出现在另一个列表中。

这不会发生在ArrayList任何其他“正常”List实现中。

“发生”的唯一方法是如果两个列表是同一个对象。

(我喜欢 JB Nizet 的“瓶中信息”类比。)

于 2013-02-01T23:28:46.073 回答