Im trying to create a student class and course class which contains an arraylist of students, and everytime a new student is added to the arraylist it should increase the number of students but it is replacing the previous student. How would I make it so it doesn't replace it?
Henry I think its not replacing the last Object in the ArrayList ,it is creating a new arrayList whenever he creates an object.
Hence there is always only one Object in the Arraylist ie the last added Object.
As Henry says.
There's nowhere that any Students are added to the ArrayList.
However, if we look at how the course and student details are printed out, you can see that only the last Student is printed...which is where the I am guessing davey is seeing it as only the last Student is printed.
First off you need to add each Student to the course.
Next you need to make sure the Student you create is inside the loop, since it isn't needed outside the loop.
Outside the loop you can then print the Course, which should include iterating over the Students in that Course and printing them out one at a time.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop