Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Stuck, looking for input

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stuck, looking for input

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: Stuck, looking for input

    The code to the other classes may be helpful...is the variable that stores the name in these classes by any chance static?

  3. #3
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Stuck, looking for input

    Arraylists have the nifty feature of working like a stack, you dont have to specify an index if you just want the next piece to fall on top of the stack. I've had tons of issues working with arraylists where I try to sort them before theyre full/when theyre empty and it always (ALWAYS) messes things up.

    Could you swap this for:

    People.add(p1);
    People.add(p2);
    People.add(p3);
    People.add(p4);

    Just to see what happens, havent got the other classes so I cant try it out for myself .

Similar Threads

  1. I am stuck
    By hawkman4188 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 29th, 2010, 12:46 AM
  2. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  3. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  4. Help I'm stuck on code.
    By GreenM in forum Loops & Control Statements
    Replies: 3
    Last Post: January 29th, 2010, 08:27 PM
  5. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 09:21 AM