4 Replies - 6702 Views - Last Post: 13 November 2010 - 05:35 AM Rate Topic: -----

#1 RunAddict  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-November 10

How to return an arraylist of objects from a method

Posted 13 November 2010 - 03:51 AM

/*This method takes in an ArrayList of Teacher objects and a experience threshold. 
   * It then iterates through the Teacher objects and identify Teachers with 
   * yearsOfExperience MORE than this experience threshold. The method returns an 
   * ArrayList of Teacher objects containing only those Teachers who meet this criteria. 
   * 
   * If no Teacher meets the criteria, this method should return an empty ArrayList. 
   * If an empty ArrayList or null is passed in, this method should return null.
   */
  public ArrayList<Teacher> getTeachersWithExperienceAbove (ArrayList<Teacher> teachers, int experienceThreshold){
    teachers = new ArrayList<Teacher>();
    
    for(int i = 0; i < teachers.size(); i++){
        Teacher myTeacher = teachers.get(i);
        int teacherExp = myTeacher.getYearsOfExperience();
        if(teacherExp > experienceThreshold){
            return myTeacher;
        }
    }
    if(teachers == null){
        return null;
    }
    else{
        return teachers;
    }
    
  }  



When i run this method, i constantly get an error that pin points to "return myTeacher" stating that it is of incompatible type. Why is this so?

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 November 2010 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to return an arraylist of objects from a method

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: How to return an arraylist of objects from a method

Posted 13 November 2010 - 04:38 AM

View PostRunAddict, on 13 November 2010 - 09:51 AM, said:

        if(teacherExp > experienceThreshold){
            return myTeacher;
        }



The problem there is that you return myTeacher which is of type Teacher, when the signature of the method says you return an ArrayList<Teacher>. What you probably want to do is to add the teachers you found to a new list, and then return it.
Was This Post Helpful? 0
  • +
  • -

#3 RunAddict  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-November 10

Re: How to return an arraylist of objects from a method

Posted 13 November 2010 - 05:04 AM

I managed to return the arraylist of objects by returning teachers instead. Implemented what you suggested, however i am unable to return the desired output which meets the criteria instead im repeatedly returning the arraylist of onjects. The method does not seem to recognise the if-else statement which i set as my condition to return the desired output.

Any inputs on how to solve this?
Was This Post Helpful? 0
  • +
  • -

#4 RunAddict  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-November 10

Re: How to return an arraylist of objects from a method

Posted 13 November 2010 - 05:32 AM

Managed to solve it already!!Thanks!!
Was This Post Helpful? 0
  • +
  • -

#5 raymens  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 02-June 10

Re: How to return an arraylist of objects from a method

Posted 13 November 2010 - 05:35 AM

Edit: nevermind, hadn't seen the problem solved reply yet

This post has been edited by raymens: 13 November 2010 - 05:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1