Trying to write an equals method for a class containing array of objects Im trying to write a College class equals method that compare the content of the arrays of the two objects of college class. So The method will compare the Student[] array in college object to other Student[] array in another college object and same for the teacher[] array. I have written an equals method for this, but it looks too long and not pretty. Is there a better way to write it?

College Class

public class College
{
  private Student[] student;
  private Teacher[] teacher;
  public College()
  {
    student = new Student[9];
    teacher = new Teacher[9];
  }

  public Student[] getStudent()
  {
    return student;
  }

  public void setStudent(Student[] student)
  {
    this.student = student;
  }

  public Teacher[] getTeacher()
  {
    return teacher;
  }

  public void setTeacher(Teacher[] teacher)
  {
    this.teacher = teacher;
  }

  public boolean equals(Object inObj)
  {// this equals method

    boolean isEqual = false;
    College inCollege = (College)inObj;

    if(student.length == inCollege.getStudent().length)
    { 
      for(int i = 0; i &lt; student.length; i++)
      {
        if(student[i].equals(inCollege.student[i]))
        {
            isEqual = true;
        }
      }
    }
    if((teacher.length == inCollege.getTeacher().length) &amp;&amp; isEqual == true)
    {
      isEqual = false;
      for(int i = 0; i &lt; inCollege.getTeacher().length; i++ )
      {
        if(teacher[i].equals(inCollege.teacher[i]))
        { System.out.println("im in");
          isEqual = true;
        }
      }
    }
    return isEqual;
  }
}


Student Class

public class Student
{
  private String name;
  private int age;

  public Student()
  {
    name = "Samrah";
    age = 19;
  }

  public Student(String name, int age)
  {
      this.name = name;
      this.age = age;
  }
  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public int getAge()
  {
    return age;
  }

  public void setAge(int age)
  {
    this.age = age;
  }

  public boolean equals(Object inObj)
  {
    boolean isEqual = false;
    if(inObj instanceof Student)
    {
        Student inStudent = (Student)inObj;
        if(this.name.equals(inStudent.getName()))
          if(this.age == inStudent.getAge())
              isEqual = true;


    }
    return isEqual;
  }

  public String toString()
  {
    String str;

    return str = "name: "+name+" age: "+age; 
   }

}


Teacher Class

public class Teacher
{
  private String name;
  private int age;

  public Teacher()
  {
    name = "Sanjay";
    age = 45;
  }

  public Teacher(String name, int age)
  {
    this.name = name;
    this.age = age;
  }

   public void setName(String name)
  {
    this.name = name;
  }

  public String getName()
  {
    return name;
  }

  public int getAge()
  {
    return age;
  }

  public void setAge(int age)
  {
    this.age = age;
  }

  public boolean equals(Object inObj)
  {
    boolean isEqual = false;
    if(inObj instanceof Teacher)
    {
       Teacher inTeacher = (Teacher)inObj;
        if(this.name.equals(inTeacher.getName()))
          if(this.age == inTeacher.getAge())
              isEqual = true;


    }
    return isEqual;
  }

  public String toString()
  {
    String str;

    return str = "name: "+name+" age: "+age; 
   }
}


Trying to do something like this:

    boolean isEqual = false;

    if(inObject instanceof EngineClass)
    {
        EngineClass inEngine = (EngineClass)inObject;
        if(cylinders == inEngine.getCylinders())
        if(fuel.equals(inEngine.getFuel()))
            isEqual = true;
    }
    return isEqual;


but with since its an array I have to loop through the arrays so I'm really confused.
It depends on what is exactly possible in your class - can you override class, can some fields be null at any point? For example retrofit creates classes using reflection and some classes can be very unexpectedly null unless set in constructor without parameters.
Many IDEs have generators, they do this really well.
If you override equals, you really should override hashCode too:
https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java


I included equals generated by Android Studio - 2 possibilities, depending on if you are okay with subclasses too (hashcodes both same).

    @Override
    public boolean equalsV1(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        College college = (College) o;
        return Arrays.equals(student, college.student) &amp;&amp;
                Arrays.equals(teacher, college.teacher);
    }

    @Override
    public boolean equalsV2(Object o) {
        if (this == o) return true;
        if (!(o instanceof College)) return false;
        College college = (College) o;
        return Arrays.equals(student, college.student) &amp;&amp;
                Arrays.equals(teacher, college.teacher);
    }

    @Override
    public int hashCode() {
        int result = Arrays.hashCode(student);
        result = 31 * result + Arrays.hashCode(teacher);
        return result;
    }



You can notice, that there are static methods for arrays in Array class.
Code is trying to exit as soon as it is sure, that equality isn't there rather than your approach, where code runs till the end and maybe it will set equals to true along the way, which has to run longer.
You don't need to use getters, you can use fields directly since it's accessing fields of different instance, but same class.
In College class it's fine, but in Student your class would crash if name is null. It shouldn't happen as even if your empty constructor you set it to something. But sadly in java I can still create Student class and pass null myself (would be good to do null check there). So just to be safe, equals should be able to handle that. There's method for that to make it easier - Objects.equals(name, other.name) rather than name.equals(other.name). First one will work fine and return false when name is null. Second one will raise NullPointerException. 
equals shouldn't ever throw Exception. If you aren't sure to guarantee that, it might be better to just surround code in try-catch and return false on exception.
Unless it's exercise, try to rely on these generators. They are pretty good and worked well for me. 
Now I switched to kotlin, that has data classes, which automatically have equals and hashCode based on all class properties already saves work and lines of code!