#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Returning objects from arraylist


    Hey guys, I am currently doing a little assignment, and I am having a little bother with it.

    I am trying to implement a method that will return all the details of all the objects stored in an ArrayList. Here is what I have:

    Code:
     ArrayList<Employee> employeeList = new ArrayList<Employee>();  
    
    public void addEmployee(Employee newGuy)
       {
          employeeList.add(newGuy);
       }
       
       public ArrayList<Employee> getEmployees()
       {
         return System.out.print(employeeList);
       }
    It flags the
    Code:
    return System.out.print(employeeList);
    saying that it is incompatible types, and that ArrayList<Employee> is required, but Void was found.
    The code, when working should print out the details of each Employee in employeeList. There is a method in the Employee class that is like a toString. It is named getDescription. I'm not sure if its maybe something to do with not having a toString set up.

    Just tried the following as well:
    Code:
     public ArrayList<Employee> getEmployees(){
          for(Employee employee : employeeList)
          {
            return System.out.print(employee);
          }
       }
    This did not work either, so its clear I am going wrong somewhere with the return statement. Hopefully you guys can help. If you need to know anything else, just ask.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    method that will return all the details of all the objects stored in an ArrayList
    What exactly is the method supposed to return? Another arraylist that contains what?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    What exactly is the method supposed to return? Another arraylist that contains what?
    The method should return all objects of the Employee class that are stored in the arraylist employeeList.

    It should return name and age, which is in the class person + salary and employeeID from the Employee class, which is a sub class of person.

    Sorry if this still doesn't clear things up.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    The method should return all objects of the Employee class that are stored in the arraylist employeeList.
    How does it return those objects? Does it copy all the objects to a new arraylist and return that? Why make a copy instead of using employeeList?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    How does it return those objects? Does it copy all the objects to a new arraylist and return that? Why make a copy instead of using employeeList?
    Well Employee objects will be added to the employeeList by the addEmployee method. I then want the getEmployee method to display all objects in employeeList. I thought this could be done in a kind of toString way where you just try printing each objects details to screen, perhaps with the for each statement I gave. I only have one arraylist and that is employeeList, or at least I only intended on using the one arraylist, employeeList.

    Sorry if I seem vague or I am making little sense. I'm not that sure what I'm doing, to be perfectly honest. If you don't understand what I'm trying to do, then perhaps you could explain how you would go about displaying each object that is stored in employeeList.

    Thanks for taking the time to help, by the way.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    want the getEmployee method to display all objects in employeeList.
    The name is confusing. A method named get... is expected to return a value.
    Your method sounds like it should be named: showEmployees() and be defined to return void.

    If the Employee class had a toString() method then printing each object in the arraylist would display the String returned by the class's toString() method.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    The name is confusing. A method named get... is expected to return a value.
    Your method sounds like it should be named: showEmployees() and be defined to return void.

    If the Employee class had a toString() method then printing each object in the arraylist would display the String returned by the class's toString() method.
    Yeah, what you say does make sense. I can certainly see why it could be confusing now. I am just following the javadoc supplied by my lecturer. In the method detail section for this particular method, it stated:

    getEmployees
    public java.util.ArrayList<Employee> getEmployees()

    Perhaps I have misunderstood the desired outcome of the method? Perhaps displaying the details of each Employee class is not what is wanted? Although it doesn't say what it it is expected to do, so I just seen getEmployee and assumed I was to return the details when the method was called.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    If that is how the method is defined, it is supposed to return an arraylist. But from where and how are the contents of the arraylist determined?
    If it is a class method, it might be a getter for the arraylist that the class holds. just return the class's arraylist.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    If that is how the method is defined, it is supposed to return an arraylist. But from where and how are the contents of the arraylist determined?
    If it is a class method, it might be a getter for the arraylist that the class holds. just return the class's arraylist.
    The arraylist (employeeList) should hold all objects of the Employee class that are added through the method addEmployee, I believe.

IMN logo majestic logo threadwatch logo seochat tools logo