简体   繁体   English

添加到 ArrayList 的 Java 对象未显示

[英]Java object added to ArrayList not showing

I have two Classes:我有两个班级:

Class1 which contains an ArrayList of type Class2 Class1 包含一个 Class2 类型的 ArrayList

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()然后当我遍历 getArrayList() 时似乎已经添加了对象

However I have another ArrayList of type class1 and when I iterate over this there object added does not appear?但是,我有另一个类型为 class1 的 ArrayList,当我遍历此对象时,添加的对象没有出现?

I thought that since objects are by reference it should be added to the ArrayList of type class1.我认为由于对象是按引用的,因此应该将其添加到 class1 类型的 ArrayList 中。 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();当我迭代subject.getTasks(); its there but when I iterate over subjectsList the new task is not there anymore.它在那里,但是当我遍历 subjectList 时,新任务不再存在。

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而在所有对象进行迭代的代码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");
   }

So you have two lists, and you add an object to one of these two lists, and expect to find it in the second one.所以你有两个列表,你将一个对象添加到这两个列表之一,并希望在第二个列表中找到它。 Why would it be in the second list?为什么会出现在第二个列表中? They're two different lists.他们是两个不同的列表。

Java objects are like real objects. Java 对象就像真实的对象。 If you put a message in a bottle, the message won't be in all the other bottles.如果你把一条信息放在一个瓶子里,这个信息就不会出现在所有其他瓶子里。 Only in the one where you put it.只有在你放置它的地方。

If you create 2 variables pointing to the same object, then both references will contain that value, example:如果您创建 2 个指向同一个对象的变量,则两个引用都将包含该值,例如:

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

both class1 and class1second contain the object class1 和 class1second 都包含对象

In the case of如果是

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

only one object contains the value.只有一个对象包含该值。

I'm guessing it's an scope problem.我猜这是一个范围问题。 Try just using This.Class1Object.getArrayList().add(object);尝试只使用This.Class1Object.getArrayList().add(object); because maybe you added the objects to a localized version of the array.因为也许您将对象添加到数组的本地化版本中。

I don't get the question.我不明白这个问题。 Its hard to understand.很难理解。

But if you mean that a second instance of Class1Object has the same ArrayList or not the some one you should watch out for static.但是,如果您的意思是 Class1Object 的第二个实例具有相同的 ArrayList 或不是您应该注意静态的那个实例。

If your variable for the ArrayList is static then all instances of these Class share the same instance of ArrayList!如果 ArrayList 的变量是静态的,那么这些 Class 的所有实例都共享相同的 ArrayList 实例!

Avoid using static unless its necessary.除非必要,否则避免使用静态。

If this does not solve your problem, provide more sample code.如果这不能解决您的问题,请提供更多示例代码。

It seems like you are saying that you have two lists.您似乎是在说您有两个列表。 And you are expecting an object added to one of the lists to automatically appear in the other list.并且您希望添加到其中一个列表的对象自动出现在另一个列表中。

That won't happen with ArrayList or any other "normal" List implementation.这不会发生在ArrayList或任何其他“正常”的List实现中。

The only way that would "happen" would be if the two lists were the same object. “发生”的唯一方法是如果两个列表是同一个对象。

(I Like JB Nizet's "message in a bottle" analogy.) (我喜欢 JB Nizet 的“瓶子里的信息”类比。)

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

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