Java对象添加到ArrayList没有显示 [英] Java object added to ArrayList not showing

查看:130
本文介绍了Java对象添加到ArrayList没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:

1级,其中包含类型的Class2的ArrayList

当我尝试添加一个新的对象如下:

 的Class2对象=新的Class2();
。Class1Object.getArrayList()加(对象);

然后,它似乎是,当我遍历getArrayList对象已被添加()

不过,我有型的Class1的另一个ArrayList的,当我遍历这个加有没有出现反对呢?

我认为,因为对象是通过引用应添加到类型为Class 1的ArrayList中。任何一个可以解释这个吗?

 公共类主题实现Serializable {私有静态最后的serialVersionUID长1L =;
私人字符串主旨名称;
私人诠释小时;
私人诠释分钟;
私人的ArrayList<任务>任务;
私人SimpleDateFormat的日期;公共科目(字符串主旨名称){
    this.subjectName =主旨名称;
    小时= 0;
    分钟= 0;
    任务=新的ArrayList<任务>();
    日期= NULL;
}
公众的ArrayList<任务> getTasks(){
    返回任务;
}

}

 公共类任务实现Serializable {    私有静态最后的serialVersionUID长= 2L;
    私人字符串描述;
    私人布尔isCompleted;    公众任务(字符串描述){
        this.description =描述;
        isCompleted = FALSE;
    }}

于是我有:

 的ArrayList<个体GT; subjectsList =新的ArrayList<个体GT;();

然后我想一个新的任务添加到特定主题,所以我做的:

 工作任务=新的任务(介绍);
ArrayList的<任务>任务列表= subject.getTasks();
taskList.add(任务);

而当我遍历 subject.getTasks(); 它的存在,但是当我遍历subjectsList新的任务是不存在了。

下面是第一个循环,显示了新的任务:

 的(任务任务:subject.getTasks()){
   的System.out.println(task.toString());
}

而code从 subjectsList

遍历所有对象

 的(主体s:subjectsList){
 对于(任务T:s.getTasks()){
   的System.out.println(t.toString());
    }
}束束= getIntent()getExtras()。
  如果(捆绑!= NULL){
    主题=(主题)bundle.get(selected_subject);
    subjectsList =(ArrayList的<个体GT;)bundle.get(subjects_list);
   }


解决方案

所以,你有两个列表,并且添加的目的是这两个列表中的一个,并期望找到它的第二个。它为什么会成为第二个列表?他们是两个不同的列表。

Java对象都是像真正的对象。如果你在一个瓶子放的消息,该消息不会在其他所有的瓶。只有在之一,你把它。

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");
   }

解决方案

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. 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.

这篇关于Java对象添加到ArrayList没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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