Results 1 to 4 of 4
  1. #1
    mBull is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default add object to ArrayList (object is from extends other class)

    hi

    i'm trying do develop a school

    i created afew classes already:
    school.class
    person.class
    student.class extends person
    employee.class extends person

    in the school class i want to create a method to add a student to the arraylist of persons and a method that only counts the objects wich are from the student class and another method wich is used to count the object from the employee class.

    Java Code:
    public class School {
        private ArrayList<Persons> persons = new ArrayList<Persons>();
    
    	public void addemployee (Employee employee){
    		if (employee !=null){
    			personen.add(employee);
    			}
    // i think this doesn't work?
    	}
    
    
    
    public int countEmployee{
    //don't know how to count these
    }
    Can you give me some hints?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    13

    Default

    There are several ways to do what you want to do. Here are a few.

    Create separate lists for employees and students. You can combine the lists on demand in a getPersons() method.

    Add counters for employees and students. Update the counters in the addEmployee(), addStudent(), etc. methods.

    Loop through the list to count students and employees on demand.

  3. #3
    mBull is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    yes, i had come to that solution aswell but I realy want only just one list of persons (the object student is sligthly different from the object employee) so isn't it possible for java to recognice each object?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    13

    Default

    If you have one list of Person, you can use the instanceof operator to determine which sub-type you are dealing with.

    With two lists, you implement a method like this:

    Java Code:
    public List<Person> getPersons() {
      final List<Person> personList = new ArrayList<Person>();
      personList.addAll(studentList);
      personList.AddAll(employeeList);
      return personList;
    }
    This approach allows for much more flexibility as your design grows. You should always create a new List and return it, rather than the instance object, as the caller can modify the list you return.

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  2. Object ArrayList - increment solution needed badly!!
    By rugbyGeek in forum New To Java
    Replies: 4
    Last Post: 03-08-2008, 01:47 AM
  3. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 07:25 PM
  4. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:57 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •