Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array List Search

Tags:

java

arraylist

I'm doing some past exam papers in preparation for a forthcoming exam, and have come across this question, and I'm not sure how to solve it, I've written a for-each loop with an if statement but I'm not sure what the header would be and what to return. Any help would be great. Thanks.

The question:

An ArrayList named classList is used to store Student objects.

Write code for a method that determines whether or not data for a student with given forename and surname is present in classList.

like image 367
Jimmy Avatar asked Dec 11 '25 09:12

Jimmy


1 Answers

Something like:

boolean lala( Student given ) {
  for( Student s : classList ) {
    if( s.getForename().equals( given.getForename() ) &&
        s.getSurname().equals( given.getSurname() ) ) {
      return true;
    }
  }

  return false;
}
like image 82
Mike Kwan Avatar answered Dec 12 '25 22:12

Mike Kwan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!