Hi, guys-
I'm having a bit of trouble with the my ArrayList. Basically, what I need to do is put in four persons (a class I developed along with Student and Instructor) into an ArrayList for a homework assignment. Then, using a simple loop I need to take it out and get the names. For some reason, "Dr. Tabatt" shows up as being in all four indexes... why is this? What am I doing wrong?
import java.util.ArrayList; public class PeopleApp { public static void main(String[] args) { Person p1 = new Person("Bobby", 1988); Student p2 = new Student("Sammy", 1987, "Psychology"); Student p3 = new Student("Ronnie", 1990, "Religious Studies"); Instructor p4 = new Instructor("Dr. Tabatt", 1928, 100000); Person x; ArrayList<Person> People; People = new ArrayList<Person>(); People.add(0, p1); People.add(1, p2); People.add(2, p3); People.add(3, p4); for (int i = 0; i < 4; i++) { x = People.get(i); System.out.println(x.getName()); } } }
Let me know if you need the Person, Student and Instructor code... getName() is supposed to return the name of the Person/Student/Instructor.
-Thanks in advance