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");
   }
user1424720
  • 61
  • 1
  • 10
  • How am I, I find a subject from `subjectsList`. I then get this `subjects` `taskList` and add a new `Task` to it. – user1424720 Feb 01 '13 at 23:35
  • 1
    Where's the code that iterates over `subjectsList` ? – NullUserException Feb 01 '13 at 23:36
  • It's just a double `for each` loop. First iterating over Subjects and then iterating over each `task`. – user1424720 Feb 01 '13 at 23:38
  • 1
    Are you sure you're not creating a new instance of `Subject` with something like `Subject subject = new Subject(...)` prior to calling `subject.getTasks()`, ie: are you sure the `Subject` you're adding tasks to belongs to the `subjectsList`? – NullUserException Feb 01 '13 at 23:39
  • 1
    Judging by the code so far, it *should* work. My bet is on a scope issue or like I said in the comment above, that you aren't actually adding tasks to subjects that are in the list. The code where `Subject subject` is defined would help. – NullUserException Feb 01 '13 at 23:43
  • Yes I'm quite sure as I don't call a constructor. I as pass the subject to another class where I want to add the new task. I do make a cast to get the object back though. But this should not matter. – user1424720 Feb 01 '13 at 23:44
  • I've used this aproach a few times before and its always worked. Though just to add I am serialising the ArrayList of subjects to a file and I'm developing this for an Android app. Neither should really affect this. – user1424720 Feb 01 '13 at 23:47
  • Here is the code for the assignment. I pass the Subject and subjectList to another activity with via Intents. The casted subject is of private scope for that class. edit: adding code to the first comment. – user1424720 Feb 01 '13 at 23:49
  • Ok's thank you for your advice. I'll check if is is indeed a Subject which is in the list. But as mentioned before I never call `new Subject()` – user1424720 Feb 01 '13 at 23:56
  • I've managed to fix this now. Thank you. – user1424720 Feb 02 '13 at 00:55

5 Answers5

4

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

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.

zibi
  • 3,183
  • 3
  • 27
  • 47
0

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.

cleong
  • 220
  • 1
  • 9
0

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.

Zarathustra
  • 2,853
  • 4
  • 33
  • 62
0

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216