简体   繁体   中英

Java object added to ArrayList not showing

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.

If you create 2 variables pointing to the same object, then both references will contain that value, example:

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

both class1 and class1second contain the object

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

If your variable for the ArrayList is static then all instances of these Class share the same instance of 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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