CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2018

    Red face array split newbie help needed

    Hi, I need some help. I have removed seperate add.member sections into one but want to be able to choose between whether student or teacher gets added and not have to input both. I assume I have to use a List Array but I have done a lot of reading and don't know where to start. Any help would be great, am very new to Java. Thanks.

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    public class Committee
        private String name;
        private List<Object> members;
        public Committee (String name)
            this.name = name;
            members = new ArrayList<Object>();
        public void addMember(Student student, Teacher teacher)
        public void printMembership()
            System.out.println("Membership of the "+name+" Committee ");
            Iterator<Object> it = members.iterator();
            while (it.hasNext()) {
                Object member = it.next();
    Last edited by 2kaud; January 11th, 2018 at 01:43 PM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: array split newbie help needed

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#']

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  3. #3
    Join Date
    Jun 1999
    Eastern Florida

    Re: array split newbie help needed

    want to be able to choose between whether student or teacher gets added and not have to input both.
    Use method overloading for that: One method for student and one for teacher.
    Method overloading In a class, there can be several methods with the same name. However they must have a different signature.

  4. #4
    Join Date
    Feb 2017

    Re: array split newbie help needed

    Quote Originally Posted by bobhope View Post
    don't know where to start
    Since you've declared the List (and its ArrayList implementation) to hold objects of type Object you can store anything in it. It's because the Object class is the superclass of every other Java class. It's the very top class of the whole Java class hierarchy. It means that technically you could do this,
        public void addMember(Object studentOrTeacherOrWhatever) { // stores anything and everything
    This will work but is not very good Object Oriented (OO) programming. Instead Student and Teacher should share (extend or implement) a common supertype (a class or an interface), say SchoolPerson. The SchoolPerson type would then replace Object in your code. So this is the proper OO way to do it (known as subtype polymorphism),
        private List<SchoolPerson> members;
    // ...
        members = new ArrayList<SchoolPerson>();
    // ...
        public void addMember(SchoolPerson person) { // stores any subtype object of SchoolPerson
            members.add(person);                   // such as Student and Teacher
    When you use the System.out.println() method on an object, Java will use the toString() method of class Object to figure out what to actually print. Then what gets printed is some obscure internal representation of the object and that usually isn't what you want. One way out of this is to override toString() in subclasses and return something more appropriate of your own choosing. But the better OO approach is to define a print method in SchoolPerson which is then overridden in the Student and Teacher subclasses.

    Finally rather than using an Iterator to scan a List have a look at the enhanced for-loop. It's much more convenient.
    Last edited by wolle; January 14th, 2018 at 12:46 AM.

Tags for this Thread

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.